I am in the process of updating an old application to use socket.io 1.4 instead of 0.9 (and express 4 instead of 3) but I am unable to get socket.io to start up. In my old app I would set up things as follows:
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
server.listen(8080);
According to the 1.x docs I thought this should work:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
server.listen(8080);
I can't figure out what I am missing. Tried to add io.listen(server) as well in case passing the server wasn't enough, but that has no effect either. I don't think it is the syntax which is wrong... but I am obviously missing some step to get this working as before.
I'll try to supply a useful answer here so you can close up the question.
Your second block of code is completely consistent with this combination shown in the socket.io doc. The only difference is that they use the .Server()
constructor and you use the .createServer()
method which (if you look at the code) do the same thing:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8080);
So, I don't think this is where the issue is. Some things to check:
Edit: As the OP discovered in comments when investigating some other things I asked, the socket.io server was starting just fine - the OP was just expecting to see some console output that used to be there before the 1.x version.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다