Express+socket.io

12830 단어 node

(app.js)


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

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

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

(index.html)



<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>
  var socket = io('http://localhost');<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>
  socket.on('news', function (data) {<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>
    console.log(data);<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>
    socket.emit('my other event', { my: 'data' });<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>
  });<span style="border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;"></span>

      



function onConnect(socket){

  // sending to the client       
  socket.emit('hello', 'can you hear me?', 1, 2, 'abc');

  // sending to all clients except sender               
  socket.broadcast.emit('broadcast', 'hello friends!');

  // sending to all clients in 'game' room except sender   “  ”        ,     
  socket.to('game').emit('nice game', "let's play a game");

  // sending to all clients in 'game1' and/or in 'game2' room, except sender   “game1” / “  2”        ,     
  socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");

  // sending to all clients in 'game' room, including sender   “  ”        ,     
  io.in('game').emit('big-announcement', 'the game will start soon');

  // sending to all clients in namespace 'myNamespace', including sender       “myNamespace”      ,     
  io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');

  // sending to individual socketid (private message)      socketid(    )
  socket.to().emit('hey', 'I just met you');

  // sending with acknowledgement     
  socket.emit('question', 'do you think so?', function (answer) {});

  // sending without compression     
  socket.compress(false).emit('uncompressed', "that's rough");

  // sending a message that might be dropped if the client is not ready to receive messages    ,            ,      
  socket.volatile.emit('maybe', 'do you really need it?');

  // sending to all clients on this node (when using multiple nodes)             (      )
  io.local.emit('hi', 'my lovely babies');

};

좋은 웹페이지 즐겨찾기