我在节点
中有以下项目结构在app.js中,假设我有一个像这样的app.js
'use strict'
const express = require('express');
const realtime = require('./controllers/realtime');
const app = express();
const server = require('http').Server(app);
var sessionMiddleware = session(sessionConfig);
realtime(server,sessionMiddleware);
在controllers / realtime.js
中"use strict";
const config = require("../config");
module.exports = (server,sessionMiddleware) => {
const io = require('socket.io')(server);
io.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
io.on('connection', (socket) => {
socket.on('statusConnetion',(data)=>{
console.log(data)
});
socket.on('disconnect', function () {
console.log(socket.id,"Un socket se desconecto");
});
console.log(`New socket connection: ${socket.id}`);
});
}
in controllers / cargos.js
const express = require('express');
const router = express.Router();
let cargos = {};
cargos.update = (req, res, next) =>{
//How can I use sockets here?
}
module.exports = cargos;
如何在文件cargos.js和其他控制器中使用套接字?
答案 0 :(得分:0)
为每个控制器使用单独的端点,如:
var io = require('socket.io').listen(80);
var user_controller = require('./controllers/user');
var chat_controller = require('./controllers/chat');
var user= io
.of('/user')
.on('connection', function (socket) {
user_controller.userData(user,socket);
});
var chat = io
.of('/chat')
.on('connection', function (socket) {
chat_controller.chatData(chat,socket);
});
And in controller simply export the module
module.exports.chatData = function(endpoint,socket){
// this function now expects an endpoint as argument
socket.on('chat',function(newsreel){
// as is proper, protocol logic like
// this belongs in a controller:
endpoint.emit(chatreel); // broadcast chat to everyone subscribing
// to our endpoint/namespace
});
}
通过分离端点,您可以在多个控制器中使用socketio
答案 1 :(得分:0)
您不仅可以导出启动服务器的函数,还可以导出处理所有socket.io连接和功能的类。这个类将是一个单独的,并且将具有使用该连接的函数,并且可以在不同的模块中使用。
例如: app.js:
'use strict'
const express = require('express');
const realtime = require('./controllers/realtime');
const app = express();
const server = require('http').Server(app);
var sessionMiddleware = session(sessionConfig);
realtime.connect(server,sessionMiddleware);
realtime.js:
let connection = null;
class Realtime {
constructor() {
this._socket = null;
}
connect(server,sessionMiddleware) {
const io = require('socket.io')(server);
io.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
io.on('connection', (socket) => {
this._socket = socket;
this._socket.on('statusConnetion',(data)=>{
console.log(data)
});
this._socket.on('disconnect', function () {
console.log(socket.id,"Un socket se desconecto");
});
console.log(`New socket connection: ${socket.id}`);
});
}
sendEvent(event, data) {
this._socket.emit(event, data);
}
registerEvent(event, handler) {
this._socket.on(event, handler);
}
static init(server,sessionMiddleware) {
if(!connection) {
connection = new Realtime();
connection.connect(server,sessionMiddleware);
}
}
static getConnection() {
if(!connection) {
throw new Error("no active connection");
}
return connection;
}
}
module.exports = {
connect: Realtime.init,
connection: Realtime.getConnection
}
cargos.js:
const express = require('express');
const router = express.Router();
let cargos = {};
cargos.update = (req, res, next) =>{
const connection = require('./controllers/realtime').connection();
connection.sendEvent("update", {params: req.params});
}
module.exports = cargos;