我正在使用以下代码在我的浏览器上测试websockets:
this.webSocket = new WebSocket("ws://echo.websocket.org");
this.webSocket.onopen = function(evt) {
cc.log("Send Text WS was opened.");
};
this.webSocket.onmessage = function(evt) {
this.socketSendTextTimes++;
var textStr = "response text msg: " + evt.data + this.socketSendTextTimes;
cc.log(textStr);
};
代码运行良好,但如果我连接到运行以下代码的我自己的服务器:
var http = require('http');
var io = require('socket.io');
var server = http.createServer(function(req, res) {
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello!</h1>');
});
var socket = io.listen(server);
socket.set('destroy upgrade', false);
socket.on('connection', function(client) {
client.on('message', function(event) {
console.log('Received message from client!', event);
});
client.on('disconnect', function() {
console.log('Server has disconnected');
});
});
server.listen(8080);
console.log('start to listen');
我的浏览器显示:
hello!
但是监听套接字没有做任何事情。如何使用websockets连接到Socket.IO服务器?
答案 0 :(得分:0)
Socket.IO使用替代传输方法而不是本机websocket,即使在模拟websockets本身时也是如此。您将需要客户端库连接到Socket.IO套接字。