最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS push server sendreceive - Stack Overflow

programmeradmin0浏览0评论

I am trying to set up a node js server to do push notifications to my browser app. I have a basic example working, but I am wondering how to send data up to the server from the client on handshake.

I Need to send to the server something like a user id, so when a notification es in for them, it can be routed back to the user.

my server looks something like this

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs');

app.listen(8000);

function handler ( req, res ) {
    res.writeHead( 200 );
    res.end('node working');
};

io.sockets.on( 'connection', function ( socket ) {

  socket.volatile.emit( 'notification' , "blah" );
});

and my client looks something like this

 var socket = io.connect('http://localhost:8000');

  socket.on('notification', function (data) {
    //prints data here
  });

I am trying to set up a node js server to do push notifications to my browser app. I have a basic example working, but I am wondering how to send data up to the server from the client on handshake.

I Need to send to the server something like a user id, so when a notification es in for them, it can be routed back to the user.

my server looks something like this

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs');

app.listen(8000);

function handler ( req, res ) {
    res.writeHead( 200 );
    res.end('node working');
};

io.sockets.on( 'connection', function ( socket ) {

  socket.volatile.emit( 'notification' , "blah" );
});

and my client looks something like this

 var socket = io.connect('http://localhost:8000');

  socket.on('notification', function (data) {
    //prints data here
  });
Share Improve this question edited Dec 29, 2013 at 22:32 BenMorel 36.7k51 gold badges205 silver badges336 bronze badges asked Apr 9, 2013 at 3:30 gh123mangh123man 1,4141 gold badge15 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

In socket.io, the emit is essentially like any other event handler (e.g. jQuery's .on('click'...)); you declare the event and send the data. On the server, you add the .on('event', ...) to catch the request and process it.

The socket.io front page shows this example for the server:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And this for the client:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

It sounds like the part you're looking for is the socket.emit portion.

I have done this sort of thing in the past by setting a cookie on the client (which you're probably doing anyway), and then using socket.io's authorization event. You can use this event to decide whether to even accept the socket connection to the user in the first place.

io.configure(function () {
  io.set('authorization', function (handshakeData, callback) {

    var cookie = handshakeData.headers.cookie;

    // parse the cookie to get user data...

    // second argument to the callback decides whether to authorize the client
    callback(null, true);
  });
});

See more documentation here: https://github./LearnBoost/socket.io/wiki/Authorizing

Note that handshakeData.headers.cookie is just a string literal representation of the cookie, so you'll have to do your own parsing.

发布评论

评论列表(0)

  1. 暂无评论