没有互联网访问就没有 Websocket 连接 (Ubuntu)

No Websocket connection without internet access (Ubuntu)

我有一个 Spring 启动应用程序,它实现了一个 Websocket 服务器 (SockJS)。

只要托管应用程序的 Ubuntu 机器可以访问互联网,我就可以在我的本地网络上将客户端连接到该服务器。

2016-10-27 11:22:20.802 DEBUG 9390 --- [nio-8085-exec-9] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@6cb31793:org.apache.tomcat.util.net.NioChannel@231c3b0c:java.nio.channels.SocketChannel[connected local=/192.168.1.104:8085 remote=/192.168.1.106:51946]], Status in: [OPEN_READ], State out: [CLOSED]

2016-10-27 11:22:20.849 DEBUG 9390 --- [io-8085-exec-10] d.f.util.ConnectivityCheckUtil : Local network interface found: wlp2s0 192.168.1.104 2016-10-27 11:22:20.850 DEBUG 9390 --- [io-8085-exec-10] d.f.util.ConnectivityCheckUtil : Your ip is: 192.168.1.104

2016-10-27 11:22:20.850 DEBUG 9390 --- [io-8085-exec-10] d.f.i.v.b.i.w.s.BrokerWebsocketHandler : Connected ... t0obkpdo 2016-10-27 11:22:20.851 INFO 9390 --- [io-8085-exec-10] d.f.i.v.b.i.w.s.BrokerWebsocketHandler : websocketTextmessagesize = 1000000

2016-10-27 11:22:20.852 DEBUG 9390 --- [io-8085-exec-10] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@1648207f:org.apache.tomcat.util.net.NioChannel@231c3b0c:java.nio.channels.SocketChannel[connected local=192.168.1.104/192.168.1.104:8085 remote=/192.168.1.106:51947]], Status in: [OPEN_READ], State out: [UPGRADING]

2016-10-27 11:22:20.852 DEBUG 9390 --- [io-8085-exec-10] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@1648207f:org.apache.tomcat.util.net.NioChannel@231c3b0c:java.nio.channels.SocketChannel[connected local=192.168.1.104/192.168.1.104:8085 remote=/192.168.1.106:51947]], Status in: [OPEN_READ], State out: [UPGRADED]

当我拔下网络路由器上的 WAN 插头时,服务器会断开所有 WS 连接并且不允许新的连接。客户端会报错码2000'All transports failed'.

服务器会抛出这个错误:

2016-10-27 11:18:38.380 DEBUG 9390 --- [nio-8085-exec-1] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@5526ea03:org.apache.tomcat.util.net.NioChannel@5f9486ff:java.nio.channels.SocketChannel[closed]], Status in: [OPEN_READ], State out: [CLOSED]

2016-10-27 11:18:51.396 DEBUG 9390 --- [nio-8085-exec-2] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@d05fac1:org.apache.tomcat.util.net.NioChannel@5f9486ff:java.nio.channels.SocketChannel[connected local=/192.168.1.104:8085 remote=/192.168.1.106:51692]], Status in: [OPEN_READ], State out: [CLOSED]

2016-10-27 11:19:01.437 DEBUG 9390 --- [nio-8085-exec-6] o.a.coyote.http11.Http11NioProtocol : Socket: [org.apache.tomcat.util.net.NioEndpoint$KeyAttachment@7f10c230:org.apache.tomcat.util.net.NioChannel@5f9486ff:java.nio.channels.SocketChannel[connected local=192.168.1.104/192.168.1.104:8085 remote=/192.168.1.106:51693]], Status in: [OPEN_READ], State out: [CLOSED]

2016-10-27 11:19:02.141 DEBUG 9390 --- [nio-8085-exec-3] d.f.util.ConnectivityCheckUtil : Local network interface found: wlp2s0 192.168.1.104

2016-10-27 11:19:02.142 DEBUG 9390 --- [nio-8085-exec-3] d.f.util.ConnectivityCheckUtil : Your ip is: 192.168.1.104

2016-10-27 11:19:02.144 DEBUG 9390 --- [nio-8085-exec-3] d.f.i.v.b.i.w.s.BrokerWebsocketHandler : Connected ... 2cbhftw3

2016-10-27 11:19:02.144 INFO 9390 --- [nio-8085-exec-3] d.f.i.v.b.i.w.s.BrokerWebsocketHandler : websocketTextmessagesize = 1000000

2016-10-27 11:19:02.147 DEBUG 9390 --- [nio-8085-exec-3] d.f.i.v.b.i.w.s.BrokerWebsocketHandler : Session 2cbhftw3 closed because of CloseStatus[code=1011, reason=null]

2016-10-27 11:19:02.149 ERROR 9390 --- [nio-8085-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.socket.sockjs.SockJsException: Uncaught failure in SockJS request, uri=http://192.168.1.104:8085/websocket/data/421/2cbhftw3/xhr_streaming; nested exception is org.springframework.web.socket.sockjs.SockJsTransportFailureException: Failed to open session; nested exception is org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe] with root cause

java.io.IOException: Broken pipe

只要我重新插入 WAN 电缆并且路由器再次可以访问互联网,WS 连接就会再次开始工作。我已经在 Netgear 和 TP-Link.

的多个路由器上尝试过这个

我已经尝试在 ubuntu 下手动设置网络接口设置,但这没有帮助。

知道是什么原因造成的吗?

BR 丹尼尔

看来您必须设置一个静态 IP,否则系统会根据 wire shark 尝试访问 DNS 发现服务,但它没有找到,只是没有连接到预期的 IP。

刚刚解决了在服务器主机文件中添加一行的相同问题,例如:

appsrv 192.168.1.15