开发者

Sending message to Socket.IO via get request

开发者 https://www.devze.com 2023-03-28 22:05 出处:网络
I have an express node.js server serving Socket.io.I would like the ability to make get requests to the express server that will automatically send a message to a channel.

I have an express node.js server serving Socket.io. I would like the ability to make get requests to the express server that will automatically send a message to a channel.

var app = require('express').createServer()
  , io = require('socket.io').listen(app)
app.listen(80);
app.get('/:channel/:message', function (req, res) {
  //Code to create socket
  socket.emit("sent from get", {channel:req.params.channel, message:req.params.message})
});

io.sockets.on('connection', function (socket) {
  socket.on('sent from get', function (data) {
    socket.broadcast.to(data.channel).emit('channel message', { message: data.message});
  });
});

How to I create (and destroy) a socket connection in the app.get block?

(For clarity, I w开发者_JAVA百科ant to use this to send a quick message from a rails server when a particular object is saved, and have a message pushed to each appropriate user.)


io.sockets.in(req.params.channel).emit("channel message", {mes:req.params.message})

That will send a message to all users in the requested channel.


var chat = io.of('/chat').on('connection', function (socket) {
socket.emit('a message', { that: 'only', '/chat': 'will get' });
chat.emit('a message', { everyone: 'in', '/chat': 'will get' }); });

The following example defines a socket that listens on '/chat'

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号