如何在node.js中的不同路由中使用socket.io [英] How to use socket.io across diffrent routes in node.js
问题描述
我的节点js
应用程序中有不同的路由,我必须在中使用 socket.io
每个路径使我的节点
和反应js
应用程序实时。但是,我的节点js 应用程序具有以下结构。
I have different routes in my node js
application and i have to use socket.io
in every route to make my node
and react js
application realtime. But, i have the below structure of my node js application.
router.js
const express = require('express');
const router = express.Router();
const worksheetController = require('../controllers/worksheet')
const attendenceController = require('../controllers/attendence')
router.route('/worksheets')
.get(
worksheetController.getWorksheet
)
.post(
worksheetController.validateWorksheet,
worksheetController.addWorksheet,
attendenceController.markAttendence
)
router.route('/attendances')
.get(
attendenceController.getAttendance
)
module.exports = router;
server.js
const express = require('express');
const router = require('./router');
const app = express();
app.use('/api', router);
app.listen('5000', () => {
console.log('Listening on port');
});
module.exports = app;
所以,我想知道
So, I want to know
1)如果我需要使用socket.io,我是否需要使用 http
模块来创建服务器。
1) Should i need to use http
module to create a server, if i need to use socket.io.
2)我如何使用socket.io进行不同的路由。
2) How can i use socket.io for diffrent routes.
我找到的帖子与我在stackoverflow上的问题,这个,这个和这个。但我不认为,这对我有用。所以请帮助我。
I found posts that match's to my question on stackoverflow, which is this, this and this. But i don't think, that works for me. So please help me.
推荐答案
- 你可以使用
http
socket.io文档中的模块或其他模块供c> socket.io - 我不确定你的理念。但是当你想要实现socket.io时。我认为你应该运行另一个节点应用程序。 (意思是您有2个nodejs app.1用于节点http正常,1表示socket.io应用程序)。在init socket.io app
https://socket.io/docs/server-api/#new-Server-之后可以使用
。因为当您部署到生产时。你应该在代理服务器旁边运行你的socket.io应用程序(例如:nginx)。 Socket.io基本上支持多传输和协议。所以如果使用http restful。如何配置从nginx到socket.io app的连接映射,如何设置错误处理程序?。path
选项的HttpServer选项
- You can use
http
module or other module in document of socket.io for to usesocket.io
- I don't sure your idea. But when you want implement socket.io. I think you should run another node app. (Meaning you have 2 nodejs app. 1 for node http normally and 1 for socket.io app). After you can use
path
option when init socket.io apphttps://socket.io/docs/server-api/#new-Server-httpServer-options
. Because when you deploy to production. You should run your socket.io app with beside of proxy serve (ex: nginx). Socket.io basically support multi transport and protocol. So if use with http restful. How about config your connection mapping from nginx to socket.io app, how you setup error handler ?.
在你的情况下:
+创建新文件socket.js:
In your case: + Create new file socket.js:
// socket.js
var http = require('http')
var socket_io = require('socket.io')
function init_socket(app) {
const server = http.Server(app)
const io = socket_io(server, { path: 'your-path-want-for-socket-io' }) // default: /socket.io/
}
import {init_socket} from 'socket.js'
init_socket(app)
这篇关于如何在node.js中的不同路由中使用socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!