2017-11-26 06:43:05 +08:00
|
|
|
const WebSocket = require('ws');
|
|
|
|
|
|
|
|
let webSocketServer;
|
|
|
|
|
|
|
|
function init(httpServer) {
|
|
|
|
webSocketServer = new WebSocket.Server({server: httpServer});
|
|
|
|
webSocketServer.on('connection', function connection(ws, req) {
|
|
|
|
console.log("websocket client connected");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-29 07:33:23 +08:00
|
|
|
async function sendMessage(message) {
|
2017-11-26 06:43:05 +08:00
|
|
|
const jsonStr = JSON.stringify(message);
|
|
|
|
|
|
|
|
webSocketServer.clients.forEach(function each(client) {
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(jsonStr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init,
|
2017-11-29 07:33:23 +08:00
|
|
|
sendMessage
|
2017-11-26 06:43:05 +08:00
|
|
|
};
|