【发布时间】:2015-06-04 17:22:48
【问题描述】:
我正在使用 nodejs 和 socket.io 创建一个套接字服务器。问题是,在客户端和服务器上,连接事件永远不会被触发。我的客户代码是这样的:
var socket = io.connect("http://127.0.0.1:8002");
console.log(socket); // This works
socket.on('connect', function(){
console.log('connected'); // Does not show meaning event not fired
});
然后,在服务器上,这就是我得到的:
var http = require('http');
var server = http.createServer().listen(8002);
var io = require('socket.io').listen(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
var redis = require('redis');
//Configure socket.io to store cookie set by Django
io.use(function(){
io.set('authorization', function(data, accept){
if(data.headers.cookie){
data.cookie = cookie_reader.parse(data.headers.cookie);
return accept(null, true);
}
return accept('error', false);
});
// io.set('log level', 1);
// io.set('origins', 'http://127.0.0.1:8001');
});
io.sockets.on('connection', function (socket) {
console.log("Someone connected"); // Nothing shows
// Create redis client
/*client = redis.createClient();
// Subscribe to the Redis events channel
client.subscribe('notifications.' + socket.handshake.cookie['sessionid']);
// Grab message from Redis and send to client
client.on('message', function(channel, message){
console.log('on message', message);
socket.send(message);
});
// Unsubscribe after a disconnect event
socket.on('disconnect', function () {
client.unsubscribe('notifications.' + socket.handshake.cookie['sessionid']);
});*/
});
如果有帮助,我正在使用this tutorial。我做错了什么?
【问题讨论】:
标签: node.js sockets websocket socket.io