Socket.io连接不起作用

时间:2016-06-02 10:53:16

标签: javascript node.js socket.io server client

Okey所以这不是我的整个代码,但我认为这是你提出这个问题的一部分。所以我的问题是,每次socket.connected返回false时,我都无法连接到我的服务器。那么你们能帮助我吗?

客户方:

var host                   = "localhost";
var socketio               = "http://" + host + ":3000";

var io = require('socket.io-client'),
socket = io.connect(socketio);
console.log('check 1' + " " + socket.connected);
socket.on('connect', function(){
    console.log('Connected through sockets.');
});
socket.on('connect_failed', function(){
    console.log('Connection Failed');
});

服务器端:

var http = require('http');

var app = http.createServer(function(req, res) {
        console.log('createServer');
});
app.listen(3000);

var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
      console.log("go");
});

0 个答案:

没有答案