从模块使用 socket.io [英] Using socket.io from a module

查看:26
本文介绍了从模块使用 socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 sio = require('socket.io').listen(app) 在我的 server.js 文件中,但我正在调用库中的一个方法,该方法想要推送一个给客户端的消息......说api.user.pushToClient()

My sio = require('socket.io').listen(app) is in my server.js file, but I'm calling a method in a library that would like to push a message to the client... say api.user.pushToClient()

我怎样才能从那里访问 sio.sockets?也许我的结构不正确?

How am I able to access sio.sockets from there? Perhaps my structure is incorrect?

文件夹结构:

server.js

api

|--user.js

|--another.js

推荐答案

server.js 中追加这一行

module.exports.sio = sio; 

api/user.js

sio = require('../server').sio;
sio.sockets.on ...

还是我误解了这个问题?

Or did I misunderstand the question?

这篇关于从模块使用 socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆