我使用node.js来实现websocket服务器和客户端。他们之间的握手就像这样。
请求URL: ws://localhost:8015/
请求方法:获取
状态代码:101个交换协议
请求头
Cache-Control: no-cache
Connection: Upgrade
Cookie: SQLiteManager_currentLangue=2
Host: localhost:8015
Origin: http:/localhost:8080
Pragma: no-cache
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Sec-WebSocket-Key: A/knWtXFtTa5V6po8XOfjg==
Sec-WebSocket-Protocol: echo-protocol
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36响应头
Connection: Upgrade
Origin: http:/localhost:8080
Sec-WebSocket-Accept: 5OUv+g5mBPxVDug4etJfGX4lxIo=
Sec-WebSocket-Protocol: echo-protocol
Upgrade: websocket服务器正在接收从客户端发送的消息(我在控制台上记录消息),但是当服务器发送消息时,客户机中的onmessage事件不会被触发。另一件让我困惑的事情是,一旦连接打开,客户端中的onmessage事件只会被触发一次。
请帮忙.我想把服务器上的信息反馈回客户端。
编辑:
这就是我在websocket客户端处理事件的方式..。
html5WebSocketClient.connect = function(){
if(window.WebSocket != undefined){
if(connection.readyState == undefined || connection.readyState > 1)
connection = new WebSocket('ws://localhost:8015/','echo-protocol');
}
if (window.MozWebSocket) {
window.WebSocket = window.MozWebSocket;
}
connection.onopen = html5WebSocketClient.onOpen(event);
connection.onmessage = html5WebSocketClient.onMessage(event);
connection.onclose = html5WebSocketClient.onClose(event);
connection.onerror = html5WebSocketClient.onError(event);};
html5WebSocketClient.onOpen = function(event)
{
$('#some_label').text("Connected");
};
html5WebSocketClient.onClose = function(event)
{
$('#some_label').text("Disconnected");
};
html5WebSocketClient.onError = function(event)
{
$('#some_label').text("Error");
};
//This is only getting fired when connection opens
html5WebSocketClient.onMessage = function(message)
{
if(message.data != undefined)
{
alert($.parseJSON(message.data));
}
};
//Server is getting this message
html5WebSocketClient.sendMessage = function()
{
var message = {"name": value, "name": value};
connection.send(JSON.stringify(message));
};以下是我如何实现服务器..。
var http = require('http');
var WebSocketServer = require('websocket').server;
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js From HTML5\n');
}).listen(8015, "127.0.0.1");
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.send(message.utf8Data); //Client is not getting this message..?
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});发布于 2013-07-13 21:57:37
这些行对您的问题负责:
connection.onopen = html5WebSocketClient.onOpen(event);
connection.onmessage = html5WebSocketClient.onMessage(event);
connection.onclose = html5WebSocketClient.onClose(event);
connection.onerror = html5WebSocketClient.onError(event);我们来分析一下。html5WebSocketClient.onOpen(event);使用参数event (即undefined)调用onOpen,并返回undefined (onOpen不返回任何内容)。因此,connection.onopen变成了unedefined。但是connection.onopen应该是一个函数。因此,与调用这些函数不同,只需这样做:
connection.onopen = html5WebSocketClient.onOpen;
connection.onmessage = html5WebSocketClient.onMessage;
connection.onclose = html5WebSocketClient.onClose;
connection.onerror = html5WebSocketClient.onError;https://stackoverflow.com/questions/17607429
复制相似问题