Java Websocket only work on localhost

时间:2017-05-16 09:23:07

标签: javascript java websocket

I am trying to use websocket.

However I can only connect to localhost. Any remote client connecting will result in code 1006 after socket.getBasicRemote().sendText();

My server side is using java. Client side is using javascript. Java is listening on 8080 with glassfish 4.11 (Tried tomcat8 as well). I tested using 2 client and both are able to connect to 8080 with HTTP.

Server side:

@ServerEndpoint("/test")
public class Test {
    final static Logger logger = Logger.getLogger(HelloWorld.class);
    @OnMessage
    public void onMessage(Session session, String message) {
        try {
            logger.info(message);
            session.getBasicRemote().sendText(message);
        } catch (IOException ex) {
        }
    }
}

Client Side:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
        <script src="public/vendor/URI.js"></script>
        <script>
            $(document).ready(function(){
                var uri = URI.parse(window.location.href);
                var socket = new WebSocket("ws://"+uri.hostname+":"+uri.port+"/WebSocketServer/test");
                socket.onopen = function(evt){
                    console.log(evt);                        
                }
                socket.onmessage = function(evt){
                    console.log(evt.data);
                }
                socket.onclose = function(evt){
                    console.log(evt);
                }
                $("#click").click(function(){
                    socket.send("asdf");
                })
            })
        </script>
    </head>    
    <body>
        <button id="click"> click </button>
    </body>
</html>

When connecting to localhost, the server is able to send message to client

This is the dump in javascript console (localhost)

test.html:19 Event {isTrusted: true, type: "open", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}bubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: WebSocketdefaultPrevented: falseeventPhase: 0isTrusted: truepath: Array(0)returnValue: truesrcElement: WebSockettarget: WebSockettimeStamp: 192.78type: "open"__proto__: Event
test.html:23 asdf

This is the dump when connectin using remote client

test.html:19 Event {isTrusted: true, type: "open", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
test.html:26 CloseEvent {wasClean: false, code: 1006, reason: "", type: "close", target: WebSocket…}

Also I can confirm that the server did reply the client by inspecting the packet. The following is a packet captured in another endpoint similar to this

Frame 65: 113 bytes on wire (904 bits), 113 bytes captured (904 bits) on interface 0
Ethernet II, Src: Sonicwal_e1:87:e0 (00:17:c5:e1:87:e0), Dst: Micro-St_ae:a2:00 (44:8a:5b:ae:a2:00)
Internet Protocol Version 4, Src: 192.168.1.33, Dst: 192.168.0.103
Transmission Control Protocol, Src Port: 8080 (8080), Dst Port: 33962 (33962), Seq: 304, Ack: 557, Len: 59
WebSocket
    1... .... = Fin: True
    .000 .... = Reserved: 0x00
    .... 0001 = Opcode: Text (1)
    0... .... = Mask: False
    .011 1001 = Payload length: 57
    Payload
Line-based text data
    {"type":"NORMAL","payload":"sdfasdf joined the channel."}

EDIT:

Stack trace for OnOpen:

2017-05-19 10:25:09 INFO  Test:28 - java.lang.Thread.getStackTrace(Thread.java:1552)
2017-05-19 10:25:09 INFO  Test:28 - net.anthonypoon.websocket_server.Test.onOpen(Test.java:27)
2017-05-19 10:25:09 INFO  Test:28 - sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-05-19 10:25:09 INFO  Test:28 - sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-05-19 10:25:09 INFO  Test:28 - sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-05-19 10:25:09 INFO  Test:28 - java.lang.reflect.Method.invoke(Method.java:498)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:520)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.AnnotatedEndpoint.onOpen(AnnotatedEndpoint.java:570)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.TyrusEndpointWrapper.onConnect(TyrusEndpointWrapper.java:766)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.TyrusWebSocket.onConnect(TyrusWebSocket.java:155)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.TyrusWebSocketEngine$TyrusConnection.<init>(TyrusWebSocketEngine.java:821)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.core.TyrusWebSocketEngine$SuccessfulUpgradeInfo.createConnection(TyrusWebSocketEngine.java:757)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.init(TyrusHttpUpgradeHandler.java:110)
2017-05-19 10:25:09 INFO  Test:28 - org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:777)
2017-05-19 10:25:09 INFO  Test:28 - org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
2017-05-19 10:25:09 INFO  Test:28 - com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
2017-05-19 10:25:09 INFO  Test:28 - org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
2017-05-19 10:25:09 INFO  Test:28 - org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
2017-05-19 10:25:09 INFO  Test:28 - org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
2017-05-19 10:25:09 INFO  Test:28 - com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
2017-05-19 10:25:09 INFO  Test:28 - com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
2017-05-19 10:25:09 INFO  Test:28 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
2017-05-19 10:25:09 INFO  Test:28 - java.lang.Thread.run(Thread.java:745)

OnMessage:

2017-05-19 10:25:11 INFO  Test:37 - java.lang.Thread.getStackTrace(Thread.java:1552)
2017-05-19 10:25:11 INFO  Test:37 - net.anthonypoon.websocket_server.Test.onMessage(Test.java:36)
2017-05-19 10:25:11 INFO  Test:37 - sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-05-19 10:25:11 INFO  Test:37 - sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-05-19 10:25:11 INFO  Test:37 - sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-05-19 10:25:11 INFO  Test:37 - java.lang.reflect.Method.invoke(Method.java:498)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:520)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.AnnotatedEndpoint.access$100(AnnotatedEndpoint.java:87)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.AnnotatedEndpoint$WholeHandler$1.onMessage(AnnotatedEndpoint.java:619)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.TyrusSession.notifyMessageHandlers(TyrusSession.java:576)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.TyrusEndpointWrapper.onMessage(TyrusEndpointWrapper.java:871)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.TyrusWebSocket.onMessage(TyrusWebSocket.java:212)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.frame.TextFrame.respond(TextFrame.java:139)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.ProtocolHandler.process(ProtocolHandler.java:807)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.core.TyrusWebSocketEngine$TyrusReadHandler.handle(TyrusWebSocketEngine.java:562)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.onDataAvailable(TyrusHttpUpgradeHandler.java:173)
2017-05-19 10:25:11 INFO  Test:37 - org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.processDataAvailable(InputBuffer.java:488)
2017-05-19 10:25:11 INFO  Test:37 - org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.onDataAvailable(InputBuffer.java:453)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.http.io.InputBuffer.invokeHandler(InputBuffer.java:1089)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.http.io.InputBuffer.invokeHandlerOnProperThread(InputBuffer.java:1080)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.http.io.InputBuffer.append(InputBuffer.java:963)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:271)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
2017-05-19 10:25:11 INFO  Test:37 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
2017-05-19 10:25:11 INFO  Test:37 - java.lang.Thread.run(Thread.java:745)

OnClose:

2017-05-19 10:25:11 INFO  Test:54 - java.lang.Thread.getStackTrace(Thread.java:1552)
2017-05-19 10:25:11 INFO  Test:54 - net.anthonypoon.websocket_server.Test.onClose(Test.java:53)
2017-05-19 10:25:11 INFO  Test:54 - sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-05-19 10:25:11 INFO  Test:54 - sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-05-19 10:25:11 INFO  Test:54 - sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-05-19 10:25:11 INFO  Test:54 - java.lang.reflect.Method.invoke(Method.java:498)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:520)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.AnnotatedEndpoint.onClose(AnnotatedEndpoint.java:535)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.AnnotatedEndpoint.onClose(AnnotatedEndpoint.java:544)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.TyrusEndpointWrapper.onClose(TyrusEndpointWrapper.java:1251)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.TyrusWebSocket.onClose(TyrusWebSocket.java:130)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.ProtocolHandler.close(ProtocolHandler.java:469)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.TyrusWebSocket.close(TyrusWebSocket.java:260)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.core.TyrusWebSocketEngine$TyrusConnection.close(TyrusWebSocketEngine.java:853)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.close(TyrusHttpUpgradeHandler.java:298)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.onError(TyrusHttpUpgradeHandler.java:240)
2017-05-19 10:25:11 INFO  Test:54 - org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.processError(InputBuffer.java:614)
2017-05-19 10:25:11 INFO  Test:54 - org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.onError(InputBuffer.java:575)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.http.io.InputBuffer.terminate(InputBuffer.java:1016)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.http.server.Response$SuspendedContextImpl.markCancelled(Response.java:2035)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.http.server.Response$SuspendedContextImpl$SuspendCloseListener.onClosed(Response.java:2112)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.http.server.Response$SuspendedContextImpl$SuspendCloseListener.onClosed(Response.java:2100)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.NIOConnection.invokeCloseListener(NIOConnection.java:955)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.NIOConnection.notifyCloseListeners(NIOConnection.java:828)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.NIOConnection.terminate0(NIOConnection.java:561)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.transport.TCPNIOConnection.terminate0(TCPNIOConnection.java:291)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.transport.TCPNIOTransport.read(TCPNIOTransport.java:609)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleRead(TCPNIOTransportFilter.java:75)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.TransportFilter.handleRead(TransportFilter.java:173)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
2017-05-19 10:25:11 INFO  Test:54 - org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
2017-05-19 10:25:11 INFO  Test:54 - java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案