什么是状态码为200的WebSocket错误?

12

我已购买一个 WebSocket 模块并在我的 WAMP 环境中安装了它。我还有一个 PHP 脚本,可以在正确位置生成 IPC 文件,并永久循环监听事件。但是,使用这个客户端代码:

var websocket = null;

var connect = function() {
    var button = document.getElementById('connect-button');
    // This function is a convenience function, to set the content
    // of the response display
    var setResponse = function(text) {
        var element = document.getElementById('response');
        element.innerHTML = text;
    }
    // A message telling the user we started connecting is always
    // useful
    button.innerHTML = 'Connecting ...';
    // The file name `demo.ws' could in principle help in having multiple
    // websockets at a single domain name.
    //
    // It's not implemented right now, but it can be if it's needed and
    // it's not too hard.    
    //var url = "ws://websocket-example/websocket-example/websocket-example-websocket.rp";
    var url = 'ws://' + window.location.hostname + '/WebSocket/websocket-example-websocket.rp';
    // Create the websocket connection now    
    websocket = new WebSocket(url, 'standard');
    // Install the handlers, the On Open handler is triggered
    // immediately after the conection has been established
    // and a successful handshake
    websocket.onopen = function(event) {
        // Update the connection status indicator
        var element = document.getElementById('connection-status');
        var input = document.getElementById('input');
        element.innerHTML = 'Connected Now';
        element.setAttribute('class', 'online');
        // Update the button, and install a new handler to allow
        // closing the websocket connection
        button.innerHTML = 'Close';
        button.onclick = function() {
            websocket.close();
        }
        input.focus();
        input.value = '';
    }
    // On close and on error handler, this is a simple demo
    // hence the simplistic approach. Ideally this should be
    // two separate functions
    websocket.onclose =
    websocket.onerror = function(event) {
        // Update the connection status indicator
        var element = document.getElementById('connection-status');
        var input = document.getElementById('input');
        element.innerHTML = 'Offline';
        element.setAttribute('class', 'offline');
        // Update button click handler, to reconnect if requested
        button.innerHTML = 'Connect';
        button.onclick = connect;
        input.value = '';
        // Clear the response text
        setResponse('');
        // Reset the websocket global variable
        websocket = null;
    }
    // On message handler, triggered when a message is received
    websocket.onmessage = function(event) {
        // Set the response text
        setResponse(event.data);
    }
}

var send = function(message) {
    // Send a message to the server but check that the websocket
    // was connected first
    if (websocket === null)
        return;
    // It's ok so, send the message now
    websocket.send(message);
}

当连接被触发时,我的请求遇到了以下错误:

WebSocket连接到'ws://websocket-example/WebSocket/websocket-example-websocket.rp'失败:在WebSocket握手期间出错:意外的响应代码:200

并且 websocket 保持为 null。我完全不理解这个错误,因为200似乎是一个OK状态,但是我的请求仍然失败。IPC文件是在服务器启动后生成的,位于正确的位置,并且执行脚本的用户具有请求文件的必要权限。这种行为的原因是什么,如何修复它?

我们能否提供一个描述PHP中WebSocket服务器初始化的示例?该服务器是否接受来自任何来源的连接? - Axel Isouard
@AxelIsouard,自从我买了它以来,我不确定是否允许分享处理程序的源代码,而且我也没有该模块的源代码,因此为了回答你的问题,我必须依靠文本总结:在服务器上,我有一个处理程序php脚本,它会删除IPC文件(如果存在),重新创建该文件,然后进入一个无限循环,在此循环中,它会检查连接和消息并进行处理。每个循环迭代之后,它会等待250毫秒。这在作者那里有效,我甚至测试了演示,并看到WebSocket正常工作。 - Lajos Arpad
@AxelIsouard 然而,该模块是为稍微不同版本的Apache构建的。这可能是一个问题。我也给他发了电子邮件,等待他的回复。我已经非常接近在这里运行WebSockets,但却被这个错误困住了,我不知道它来自哪里以及它的含义是什么。 - Lajos Arpad
2个回答

11
在WebSocket连接中,您需要一个101 Switching Protocols状态响应。
如果收到200状态响应,则可能意味着请求未到达WebSocket处理程序。
我还建议查看开发工具,并检查响应是否具有任何WebSocket握手标头。
如果有,则可能是模块存在问题。否则,可能是您的配置出了问题。

1
这样的响应意味着位于URL的远程资源没有监听或至少无法响应websocket请求。请检查您尝试访问的远程服务。如果您编写了服务器,请发布您的源代码。

Kevin,我不确定我是否被允许发布我已经付费的代码。我已经在问题的评论部分总结了它的工作原理,但是不能在这里分享实际的服务器代码,因为我会损害一个创建了一个很好的模块的优秀人士的利益。我几乎完成了设置,但卡在了这个错误上,并想知道其含义和原因。如果我们能够缩小范围,那么我将能够轻松解决问题,然而,问题空间现在非常广泛。 - Lajos Arpad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接