简体   繁体   English

在外部库中为Express.js使用SocketIO

[英]Using SocketIO in external lib for expressjs

my app.js is started with following code 我的app.js从以下代码开始

var express = require("express"),
app     = express(),
http    = require('http'),
server  = http.createServer(app),
io      = require('socket.io').listen(server),
games   = require("./lib/games");

Now I need to use socket.io in external lib which named games,how can I do that? 现在,我需要在名为游戏的外部lib中使用socket.io ,该怎么办?

There are many ways to do this. 有很多方法可以做到这一点。 One way is to have games export a function which accepts the socket.io object as a parameter. 一种方法是让游戏导出一个接受socket.io对象作为参数的函数。

For example: 例如:

// games.js
module.exports = function(io) { ... /* do something with io */ }

// app.js
var io = require('socket.io').listen(server),
games   = require("./lib/games");
games(io);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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