【问题标题】:Jetty Websocket - sendString() triggers onClose (Error 1006 - Websocket Read EOF)Jetty Websocket - sendString() 触发 onClose(错误 1006 - Websocket 读取 EOF)
【发布时间】:2017-06-10 15:41:52
【问题描述】:

每次我的 WebSocket 向我的客户端发送消息时,客户端都会调用 onClose 函数进行连接,但不会收到消息。

错误为 1006 - Websocket 读取 EOF

我知道这个答案 getting the reason why websockets closed ,所以错误是浏览器本地的,但我使用的是 Firefox(最新)和 html 和 javascript 代码没什么特别的。

直到服务器调用 session.getRemote().sendString 或 sendStringByFuture 客户端似乎已连接。 当sendString(message)被调用时,ws.onclose总是被触发!


服务器端:

我的 Websocket.war 在带有模块部署、http、jsp、客户端和 websocket 的码头服务器上运行,我认为连接已成功建立。 (见日志)

我拿了official jetty-websocket example。 我删除了 Main.java,因为我使用 import javax.servlet.annotation.WebServlet;

映射了 websocket-server
import javax.servlet.annotation.WebServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

@SuppressWarnings("serial")
@WebServlet(value="/start", asyncSupported = true)
public class WebServerTest extends WebSocketServlet{

    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(WebSocketTest.class);
    }
}

Jetty-服务器日志:

WebSocket Connect: WebSocketSession[websocket=JettyAnnotatedEventDriver[org.el.WebSocketTest@e0bb5f],behavior=SERVER,connection=WebSocketServerConnection@4a0644db[ios=IOState@3986db[CONNECTED,!in,out],f=Flusher[queueSize=0,aggregateSize=0,failure=null],g=Generator[SERVER,validating,+rsv1],p=Parser@11f79d8[ExtensionStack,s=START,c=0,len=0,f=null]]<-SocketChannelEndPoint@694908{/myIP:somePort<->/myIP:8080,OPEN,fill=-,flush=-,to=0/300000}{io=0/0,kio=0,kro=1}->WebSocketServerConnection@4a0644db[ios=IOState@3986db[CONNECTED,!in,out],f=Flusher[queueSize=0,aggregateSize=0,failure=null],g=Generator[SERVER,validating,+rsv1],p=Parser@11f79d8[ExtensionStack,s=START,c=0,len=0,f=null]],remote=WebSocketRemoteEndpoint@1a29617[batching=true],incoming=JettyAnnotatedEventDriver[org.el.WebSocketTest@e0bb5f],outgoing=ExtensionStack[queueSize=0,extensions=[permessage-deflate],incoming=org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension,outgoing=org.eclipse.jetty.websocket.common.extensions.compress.PerMessageDeflateExtension]]

当我尝试从服务器向客户端发送答案时

Echoing back text message [start]
INFO:oe.WebSocketTest:qtp8420901-11: WebSocket Close: 1006 - WebSocket Read EOF

客户端:

Firefox 可以建立与“ws://myIP:8080/Websocket/start”的连接并接收 ws.onopen。

example.js

try {
    var ws = new WebSocket("ws://myIP:8080/Websocket/start");
} catch(Exception){ altert("Help"); }

ws.onopen = function () {
    appendLog("Connected to socket service!");
};

ws.onmessage = function (evt) {
    appendLog(evt.data);
};

ws.onclose = function () {
    appendLog("Disconnected from stock service!");
};

ws.onerror = function (err) {
    console.log("ERROR!", err);
};

function appendLog(logText) {
    var log = document.getElementById("log");
    log.value = log.value + logText + "\n";
}

function sendToServer(msg) {
    ws.send(msg);
}

【问题讨论】:

    标签: websocket jetty embedded-jetty jetty-9


    【解决方案1】:

    只是不要使用端口 8080。如果使用例如 8081,则不会出现此错误。

    【讨论】:

      【解决方案2】:

      好的,经过数小时的阅读和搜索,我终于找到了我的错误。

      我在我的办公桌上使用预装卡巴斯基的 Windows,我的同事使用没有杀毒软件的 Linux。在他的浏览器中一切正常,我的一个连接在尝试接收消息时立即关闭。

      是我的防病毒/防火墙阻止了 http-websockets。

      解决方案:我现在将 https-websockets 与“wss://myIP:8443/mySite”一起使用,并且工作正常。 一个不错的教程也是this site here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 2018-08-23
        • 1970-01-01
        • 2019-05-25
        • 2017-04-28
        相关资源
        最近更新 更多