Когда я подключаюсь к розетке через Wi-Fi, все работает нормально, вот журнал Logger:
2018-11-01 11:54:44.395 : io.socket.client.IO: ignoring socket cache for http://myserverexample.com:80/
2018-11-01 11:54:44.402 : io.socket.client.Manager: readyState CLOSED
2018-11-01 11:54:44.405 : io.socket.client.Manager: opening http://myserverexample.com:80/
2018-11-01 11:54:44.408 : io.socket.client.Manager: connection attempt will timeout after 20000
2018-11-01 11:54:44.411 : .socket.engineio.client.Socket: creating transport 'polling'
2018-11-01 11:54:44.413 : .socket.engineio.client.Socket: setting transport polling
2018-11-01 11:54:44.415 : neio.client.transports.Polling: polling
2018-11-01 11:54:44.417 : o.client.transports.PollingXHR: xhr poll
2018-11-01 11:54:44.421 : o.client.transports.PollingXHR: xhr open GET: http://myserverexample.com/socket.io/?EIO=3&transport=polling
2018-11-01 11:54:44.423 : o.client.transports.PollingXHR: sending xhr with url http://myserverexample.com/socket.io/?EIO=3&transport=polling | data null
2018-11-01 11:54:44.460 : neio.client.transports.Polling: polling got data 96:0{"sid":"jzNvWLvHoObxf-DmAC2M","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2018-11-01 11:54:44.461 : .socket.engineio.client.Socket: socket received: type 'open', data '{"sid":"jzNvWLvHoObxf-DmAC2M","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}'
2018-11-01 11:54:44.463 : .socket.engineio.client.Socket: socket open
2018-11-01 11:54:44.464 : io.socket.client.Manager: open
2018-11-01 11:54:44.464 : io.socket.client.Manager: cleanup
2018-11-01 11:54:44.465 : io.socket.client.Socket: transport is open - connecting
2018-11-01 11:54:44.466 : .socket.engineio.client.Socket: starting upgrade probes
2018-11-01 11:54:44.467 : .socket.engineio.client.Socket: probing transport 'websocket'
2018-11-01 11:54:44.468 : .socket.engineio.client.Socket: creating transport 'websocket'
2018-11-01 11:54:44.471 : neio.client.transports.Polling: polling
2018-11-01 11:54:44.472 : o.client.transports.PollingXHR: xhr poll
2018-11-01 11:54:44.474 : o.client.transports.PollingXHR: xhr open GET: http://myserverexample.com/socket.io/?EIO=3&transport=polling&sid=jzNvWLvHoObxf-DmAC2M
2018-11-01 11:54:44.475 : o.client.transports.PollingXHR: sending xhr with url http://myserverexample.com/socket.io/?EIO=3&transport=polling&sid=jzNvWLvHoObxf-DmAC2M | data null
2018-11-01 11:54:44.514 : .socket.engineio.client.Socket: probe transport 'websocket' opened
2018-11-01 11:54:44.517 : neio.client.transports.Polling: polling got data 2:40
2018-11-01 11:54:44.519 : .socket.engineio.client.Socket: socket received: type 'message', data '0'
2018-11-01 11:54:44.522 : neio.client.transports.Polling: polling
2018-11-01 11:54:44.523 : o.client.transports.PollingXHR: xhr poll
2018-11-01 11:54:44.524 : o.client.transports.PollingXHR: xhr open GET: http://myserverexample.com/socket.io/?EIO=3&transport=polling&sid=jzNvWLvHoObxf-DmAC2M
2018-11-01 11:54:44.525 : o.client.transports.PollingXHR: sending xhr with url http://myserverexample.com/socket.io/?EIO=3&transport=polling&sid=jzNvWLvHoObxf-DmAC2M | data null
2018-11-01 11:54:44.527 : .socket.engineio.client.Socket: probe transport 'websocket' pong
2018-11-01 11:54:44.528 : .socket.engineio.client.Socket: pausing current transport 'polling'
2018-11-01 11:54:44.529 : neio.client.transports.Polling: we are currently polling - waiting to pause
2018-11-01 11:54:44.634 : neio.client.transports.Polling: polling got data 1:6
2018-11-01 11:54:44.637 : .socket.engineio.client.Socket: socket received: type 'noop', data 'null'
2018-11-01 11:54:44.639 : neio.client.transports.Polling: pre-pause polling complete
2018-11-01 11:54:44.641 : neio.client.transports.Polling: paused
2018-11-01 11:54:44.642 : .socket.engineio.client.Socket: changing transport and sending upgrade packet
2018-11-01 11:54:44.643 : .socket.engineio.client.Socket: setting transport websocket
2018-11-01 11:54:44.644 : .socket.engineio.client.Socket: clearing existing transport polling
2018-11-01 11:54:44.646 : neio.client.transports.Polling: ignoring poll - transport state 'paused'
2018-11-01 11:55:09.475 : .socket.engineio.client.Socket: writing ping packet - expecting pong within 5000ms
2018-11-01 11:55:09.481 : .socket.engineio.client.Socket: flushing 1 packets in socket
2018-11-01 11:55:09.538 : .socket.engineio.client.Socket: socket received: type 'pong', data 'null'
```
Но когда я пытался подключиться через мобильный интернет, опрос получил данные в формате xml:
neio.client.transports.Polling: polling got data <?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="FEHLER" title="ERROR">
<p>
The requested item could not be loaded <br/>
Wrong MIME-Type
<do type="prev" label="back">
<prev/>
</do>
</p>
</card>
</wml>
А потом я получаю
mSocket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
if (args.length> 0 && args[0] instanceof EngineIOException){
((EngineIOException)args[0]).printStackTrace();
}
}
});
Это EngineIOException:
io.socket.engineio.client.EngineIOException: server error
at io.socket.engineio.client.Socket.onPacket(Socket.java:547)
at io.socket.engineio.client.Socket.access$1000(Socket.java:36)
at io.socket.engineio.client.Socket$5.call(Socket.java:335)
at io.socket.emitter.Emitter.emit(Emitter.java:117)
at io.socket.engineio.client.Transport.onPacket(Transport.java:126)
at io.socket.engineio.client.transports.Polling.access$700(Polling.java:18)
at io.socket.engineio.client.transports.Polling$2.call(Polling.java:127)
at io.socket.engineio.parser.Parser.decodePayload(Parser.java:222)
at io.socket.engineio.client.transports.Polling._onData(Polling.java:135)
at io.socket.engineio.client.transports.Polling.onData(Polling.java:102)
at io.socket.engineio.client.transports.PollingXHR$5$1.run(PollingXHR.java:125)
at io.socket.thread.EventThread$2.run(EventThread.java:80)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Вот полный журнал Logger через мобильный интернет:
2018-11-01 11:55:26.168 : io.socket.client.IO: ignoring socket cache for http://myserverexample.com:80/
2018-11-01 11:55:26.187 : io.socket.client.Manager: readyState CLOSED
2018-11-01 11:55:26.189 : io.socket.client.Manager: opening http://myserverexample.com:80/
2018-11-01 11:55:26.192 : io.socket.client.Manager: connection attempt will timeout after 20000
2018-11-01 11:55:26.195 : .socket.engineio.client.Socket: creating transport 'polling'
2018-11-01 11:55:26.199 : .socket.engineio.client.Socket: setting transport polling
2018-11-01 11:55:26.202 : neio.client.transports.Polling: polling
2018-11-01 11:55:26.203 : o.client.transports.PollingXHR: xhr poll
2018-11-01 11:55:26.205 : o.client.transports.PollingXHR: xhr open GET: http://myserverexample.com/socket.io/?EIO=3&transport=polling
2018-11-01 11:55:26.208 : o.client.transports.PollingXHR: sending xhr with url http://myserverexample.com/socket.io/?EIO=3&transport=polling | data null
2018-11-01 11:55:26.310 : neio.client.transports.Polling: polling got data <?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="FEHLER" title="ERROR">
<p>
The requested item could not be loaded <br/>
Wrong MIME-Type
<do type="prev" label="back">
<prev/>
</do>
</p>
</card>
</wml>
2018-11-01 11:55:26.313 : .socket.engineio.client.Socket: socket received: type 'error', data 'parser error'
2018-11-01 11:55:26.316 : .socket.engineio.client.Socket: socket error io.socket.engineio.client.EngineIOException: server error
2018-11-01 11:55:26.316 : io.socket.client.Manager: connect_error
2018-11-01 11:55:26.317 : io.socket.client.Manager: cleanup
2018-11-01 11:55:26.320 : io.socket.client.Manager: disconnect
2018-11-01 11:55:26.321 : io.socket.client.Manager: cleanup
2018-11-01 11:55:26.322 : .socket.engineio.client.Socket: socket close with reason: forced close
2018-11-01 11:55:26.323 : neio.client.transports.Polling: transport open - closing
2018-11-01 11:55:26.324 : neio.client.transports.Polling: writing close packet
2018-11-01 11:55:26.325 : o.client.transports.PollingXHR: xhr open POST: http://myserverexample.com/socket.io/?EIO=3&transport=polling
2018-11-01 11:55:26.326 : o.client.transports.PollingXHR: sending xhr with url http://myserverexample.com/socket.io/?EIO=3&transport=polling | data 1:1
2018-11-01 11:55:26.327 : .socket.engineio.client.Socket: socket closing - telling transport to close
2018-11-01 11:55:26.328 : io.socket.client.Manager: disconnect
2018-11-01 11:55:26.328 : io.socket.client.Manager: cleanup
2018-11-01 11:55:26.330 : io.socket.client.Manager: disconnect
2018-11-01 11:55:26.331 : io.socket.client.Manager: cleanup
2018-11-01 11:55:26.331 : io.socket.client.Manager: disconnect
2018-11-01 11:55:26.332 : io.socket.client.Manager: cleanup
Вот моя проблема на github
Я обнаружил , что некоторые операторы мобильной связи отображают эту страницу, если Content-Type не подходит. Поэтому я добавил заголовки в OkHttpClient:
OkHttpClient.Builder okHttpClient= new OkHttpClient.Builder()
.addNetworkInterceptor(new SocketHeadersInterceptor())
.addInterceptor(new SocketHeadersInterceptor())
;
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
Socket mSocket = IO.socket("http://myserverexample.com/", opts);
SocketHeadersInterceptor.java
public class SocketHeadersInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
builder
.header("Accept", "text/plain;charset=utf-8")
.header("Content-Type", "text/plain;charset=utf-8")
return chain.proceed(builder.build());
}
Но все равно не работает ...