以某种方式,在迁移到新服务器并更新了node.js和socket.io程序包之后,我的WebSocket连接失败。
我曾经这样做:
var app = express();
var https = require('https');
var server = https.createServer(ssl_options, app);
server.listen(55555);
global.io = require('socket.io').listen(server);
似乎没有用。
我尝试过:
var app = express();
var https = require('https');
var server = https.createServer(ssl_options, app);
server.listen(55555);
global.io = require('socket.io')();
io.attach(server,{wsEngine:'ws'});
无济于事。
我忽略了什么?从未触发过io.on('connect',function(socket){...})。
这是我做console.log(io)时打印的内容:
Server {
nsps:
{ '/':
Namespace {
name: '/',
server: [Circular],
sockets: {},
connected: {},
fns: [],
ids: 0,
adapter: [Adapter],
_events: [Object],
_eventsCount: 1 } },
_path: '/socket.io',
_serveClient: true,
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
Namespace {
name: '/',
server: [Circular],
sockets: {},
connected: {},
fns: [],
ids: 0,
adapter:
Adapter { nsp: [Circular], rooms: {}, sids: {}, encoder: Encoder {} },
_events: [Object: null prototype] { connect: [Function] },
_eventsCount: 1 },
eio:
Server {
clients: {},
clientsCount: 0,
wsEngine: 'ws',
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: { threshold: 1024 },
httpCompression: { threshold: 1024 },
ws:
WebSocketServer {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: [Object: null prototype] { connection: [Function: bound ] },
_eventsCount: 1 },
httpServer:
Server {
_contexts: [],
requestCert: false,
rejectUnauthorized: true,
key:
<Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 45 43 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a 4d 49 47 6b 41 67 45 42 42 44 41 33 36 47 77 34 6b 30 77 ... 238 more bytes>,
cert:
<Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0a 4d 49 49 45 6c 6a 43 43 42 44 75 67 41 77 49 42 41 67 49 52 41 4e ... 1600 more bytes>,
ca:
[ <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0d 0a 4d 49 49 44 71 44 43 43 41 79 36 67 41 77 49 42 41 67 49 52 41 ... 1300 more bytes>,
<Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0d 0a 4d 49 49 44 30 7a 43 43 41 72 75 67 41 77 49 42 41 67 49 51 64 ... 1358 more bytes>,
<Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0d 0a 4d 49 49 45 4e 6a 43 43 41 78 36 67 41 77 49 42 41 67 49 42 41 ... 1494 more bytes> ],
honorCipherOrder: true,
ALPNProtocols: <Buffer 08 68 74 74 70 2f 31 2e 31>,
sessionIdContext: '44a6600ba0f6b70f1fa9bf72bec6d31c',
pfx: undefined,
passphrase: undefined,
clientCertEngine: undefined,
minVersion: undefined,
maxVersion: undefined,
secureProtocol: undefined,
crl: undefined,
ciphers: undefined,
ecdhCurve: undefined,
dhparam: undefined,
secureOptions: undefined,
_sharedCreds: SecureContext { context: SecureContext {} },
_events:
[Object: null prototype] {
connection: [Function: tlsConnectionListener],
secureConnection: [Function: connectionListener],
tlsClientError: [Function: addListener],
listening: [Function],
close: [Function: bound ],
upgrade: [Function],
request: [Function] },
_eventsCount: 7,
_maxListeners: undefined,
_connections: 0,
_handle: null,
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: false,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
maxHeadersCount: null,
headersTimeout: 40000,
[Symbol(IncomingMessage)]: [Function: IncomingMessage],
[Symbol(ServerResponse)]: [Function: ServerResponse],
[Symbol(handshake-timeout)]: 120000,
[Symbol(snicallback)]: undefined,
[Symbol(asyncId)]: -1 },
engine:
Server {
clients: {},
clientsCount: 0,
wsEngine: 'ws',
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [ 'polling', 'websocket' ],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: { threshold: 1024 },
httpCompression: { threshold: 1024 },
ws:
WebSocketServer {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: [Object],
path: null,
clients: [] },
_events: [Object: null prototype] { connection: [Function: bound ] },
_eventsCount: 1 }
}