【发布时间】:2017-02-25 11:28:27
【问题描述】:
我购买了一个 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 服务器初始化吗?服务器是否接受来自任何来源的任何连接?
-
@AxelIsouard,因为我买了它,我不确定我是否可以分享处理程序的源代码,我不拥有模块的源代码,所以,回答你问题,我必须依靠文本总结:在服务器上,我有一个处理程序 php 脚本,如果存在 IPC 文件,它会删除并重新创建它,然后进入一个永久循环,在其中检查连接和消息并处理它们。在循环的每次迭代之后,它等待 250 毫秒。这对作者有效,我什至测试了演示并看到了 WebSocket 工作。
-
@AxelIsouard 但是,该模块是为稍微不同的 Apache 版本构建的。这是一个可能的问题。我也给他发了电子邮件,会看到他的回复。我非常接近让 WebSockets 在这里运行并且被这个错误困扰,我不知道它来自哪里以及它意味着什么。
标签: javascript php websocket wamp ipc