简体   繁体   中英

How to start node express, binaryserver and socket.io on same port?

I have code snippet to explain what i am doing and what i want.

var express = require('express');
var http = require('http');
var app = express();

app.use('/', express.static(__dirname + '/static'));

var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);

var binaryServer = new BinaryServer({server:server});


var ioServer = http.createServer(app);

var io = require('socket.io').listen(ioServer);

I can run node express and socket.io on same port.

ioServer.listen(8080, function(){
    console.log('server running at localhost:8080');
});

Same can be done with node express and binaryServer.

server.listen(8080, function(){
    console.log('server running at localhost:8080');
});

But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case). Any suggestions ?

You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});

server.listen(8080, function(){
  console.log('http/socket/binary server running at localhost:8080');
});

Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM