服务端发送消息

// send to current request socket client
socket.emit('message', "this is a test");

// sending to all clients, include sender
io.sockets.emit('message', "this is a test");


// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");


// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');


// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('message', 'cool game');

// sending to individual socketid
io.sockets.socket(socketid).emit('message', 'for your eyes only');

事件汇总

Server

On the server, assuming

var io = require('socket.io'),io.sockets.on('connection', function(socket) {}) 
// initial connection from a client. socket argument should be used in further communication with the client.

socket.on('message', function(message, callback) {}) 
// "message" is emitted when a message sent with socket.send is received. message is the message sent, and callback is an optional acknowledgement function.

socket.on('anything', function(data) {}) 
// "anything" can be any event except for the reserved ones.

socket.on('disconnect', function() {}) 
// the disconnect event is fired in all cases, when the client-server connection is closed. It fires on wanted, unwanted, mobile, unmobile, client and server disconnects. There is no dedicated reconnect event. You have to use the "connection" event for reconnect handling.

Client

The client doc (including event list) is available here: https://github.com/LearnBoost/socket.io-client

On the client, assuming

var socket = io.connect(host, options),socket.on('connect', function () {}) 
// "connect" is emitted when the socket connected successfully

socket.on('connecting', function () {}) 
//"connecting" is emitted when the socket is attempting to connect with the server.

socket.on('disconnect', function () {}) 
// "disconnect" is emitted when the socket disconnected

socket.on('connect_failed', function () {}) 
// "connect_failed" is emitted when socket.io fails to establish a connection to the server and has no more transports to fallback to.

socket.on('error', function () {}) 
// "error" is emitted when an error occurs and it cannot be handled by the other event types.

socket.on('message', function (message, callback) {}) 
// "message" is emitted when a message sent with socket.send is received. message is the sent message, and callback is an optional acknowledgement function.

socket.on('anything', function(data, callback) {}) 
// "anything" can be any event except for the reserved ones. data is data, and callback can be used to send a reply.

socket.on('reconnect_failed', function () {}) 
// "reconnect_failed" is emitted when socket.io fails to re-establish a working connection after the connection was dropped.

socket.on('reconnect', function () {}) 
// "reconnect" is emitted when socket.io successfully reconnected to the server.

socket.on('reconnecting', function () {}) 
// "reconnecting" is emitted when the socket is attempting to reconnect with the server.

Order of Client Events

When you first connect:

  • connecting
  • connect

When you momentarily lose connection:

  • disconnect
  • reconnecting (1 or more times)
  • connecting
  • reconnect
  • connect

Losing connection completely:

  • disconnect
  • reconnecting (repeatedly)

使用房间

Rooms

Rooms allow simple partitioning of the connected clients. This allows events to be emitted to subsets of the connected client list, and gives a simple method of managing them.

Joining and Leaving Joining a named room is achieved by calling the join() function on a connected socket object.

socket.join('room')

Leaving a room is achieved by calling the leave() function on a connected socket object.

socket.leave('room')

A simple subscribe/unsubscribe system can be built very quickly.

socket.on('subscribe', function(data) { socket.join(data.room); })
socket.on('unsubscribe', function(data) { socket.leave(data.room); })

Note that it is not necessary to call socket.leave() during the disconnect event. This will happen automatically. Empty rooms will be automatically pruned so there is no need to manually remove them.

Emitting to a room

There are two ways for emitting to a room: either using socket.broadcast.to('room')orio.sockets.in('room').

Socket Broadcast

Broadcasts are sent from a socket object and are received by all clients in the room except for the emitting socket

io.sockets.on('connection', function (socket) {
  socket.broadcast.to('room').emit('event_name', data) //emit to 'room' except this socket
})

A broadcast is sent to all sockets except for the emitting one if a room is not specified

io.sockets.on('connection', function (socket) {
  socket.broadcast.emit('event_name', data) //emit to all sockets except this one
})

Io Sockets

Emitting an event to all clients in a particular room

io.sockets.in('room').emit('event_name', data)

Emitting an event to all clients

io.sockets.emit('event_name', data)

Emitting an event to all clients in a namespace of a particular room

io.of('namespace').in('room').emit('event_name', data)

Getting information about rooms

All rooms

A list of all rooms can be found by looking in io.sockets.manager.rooms. This is a hash, with the room name as a key to an array of socket IDs. Note that the room names will have a leading / character. This is used internally and does not have to be referenced when joining, leaving or emitting to rooms.

Clients in a room

If you want a list of clients in a particular room, call io.sockets.clients('room'). This will return Socket instances of all clients in the room.

For namespaces call io.of('namespace').clients('room')

Rooms a client has joined

You can get a list of rooms a particular client socket has joined by looking inio.sockets.manager.roomClients[socket.id]. Again, the room name will have a leading / character.

It is important to note that all clients are automatically joined to a room with a blank name. This is a catch-all room that contains a list of all connected clients. It will show up as a key with a value of "" in lists of room names.

校验

https://github.com/LearnBoost/socket.io/wiki/Authorizing 大体流程就是会先执行authorization,完了之后才会触发各种事件