如何在同一端口上启动node express,binaryserver和socket.io?

时间:2022-08-22 14:36:52

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.

我可以在同一个端口上运行node express和socket.io。

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

Same can be done with node express and binaryServer.

可以使用node express和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 ?

但我想运行node express,socket.io和binaryServer在同一个端口上正在运行(在这种情况下为8080)。有什么建议么 ?

1 个解决方案

#1


1  

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

您需要将SocketIO和binaryServer连接到同一个http服务器实例,然后再启动该单个实例。

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.

设置路径,以便binaryServer不与您的任何应用程序冲突。客户端连接中也需要此路径。

#1


1  

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

您需要将SocketIO和binaryServer连接到同一个http服务器实例,然后再启动该单个实例。

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.

设置路径,以便binaryServer不与您的任何应用程序冲突。客户端连接中也需要此路径。