1. Service-side
Io.on (' Connection ', function (socket));
Listen to the client connection, the callback function will pass the connection socket
Io.sockets.emit (' String ', data);
Broadcast messages to all clients
Io.sockets.socket (Socketid). Emit (' String ', data);
Sends a message to the specified client
Socket.on (' String ', function (data));
Listen for information sent by the client
Socket.emit (' String ', data);
Send a message to the client of the socket
Broadcast messages
Broadcast Message Socket.broadcast.emit ("MSG", {data: "Hello,everyone"}) to clients other than yourself Broadcast Message Io.sockets.emit ("MSG", {data: "Hello,all"}) to all clients;
Group
Socket.on (' group1 ', function (data) { socket.join (' group1 ');}); Socket.on (' group2 ', function (data) { socket.join (' group2 ');});
Client sends
Socket.emit (' group1 '), you can join the group1 group;
Socket.emit (' group2 '), you can join the GROUP2 group;
Multiple groupings can exist for a client (subscription mode)
Kicking out a group
Socket.leave (Data.room);
Send a message to a user in a group
Do not include your own socket.broadcast.to (' group1 '). Emit (' event_name ', data);//Include own io.sockets.in (' group1 '). Emit (' Event_Name '), data);
The broadcast method allows the current socket client not to be within the group
Gets the connected client socket
Io.sockets.clients (). ForEach (socket) { //...})
Get group Information
Get all Room (group) information io.sockets.manager.rooms//to get the room information for this socketid entry io.sockets.manager.roomclients[socket.id]// Get the client in the particular room and return all socket instances in this Chamber io.sockets.clients (' particular rooms ')
Another way to group
Io.of ('/some '). On (' Connection ', function (socket) { socket.on (' test '), function (data) { Socket.broadcast.emit (' Event_Name ', {}); });
Client
var socket = io.connect (' ws://103.31.201.154:5555/some ') socket.on (' Even_name ', function (data) { Console.log ( data);})
Clients are linked to ws://103.31.201.154:5555 but the server can filter them out via Io.of ('/some ').
In addition, Socket.io offers 4 configurations of API:io.configure, Io.set, Io.enable, io.disable. Where Io.set is set for a single item, io.enable and io.disable are used for single-set Boolean configurations. Io.configure allows you to configure different parameters for different production environments, such as devlopment,test, and so on.
2. Client
Create a socket connection
var socket = io ("ws://103.31.201.154:5555");
Listening for service messages
Socket.on (' msg ', function (data) { socket.emit (' msg ', {rp: "Fine,thank You"});//Send message to Server console.log (data);});
Socket.on ("String", Function (data)) listens to the message sent by the server sting parameter is the same as the first parameter of the service-side emit
Monitor socket disconnect and re-connect.
Socket.on (' Disconnect ', function () { Console.log ("Break with Service");}); Socket.on (' Reconnect ', function () { Console.log ("Reconnect to Server");});
The client Socket.on () listens for events:
Connect: Successful Connection
Connecting: Connecting
Disconnect: Disconnecting
Connect_failed: Connection Failed
Error: Errors occur and cannot be handled by other event types
Message: Same server-side message Event
Anything: Same server-side anything event
Reconnect_failed: Re-connect failed
Reconnect: Successful re-connect
Reconnecting: being re-connected
When the first connection occurs, the event firing order is:connecting->connect; when the connection is lost, the event firing order is: disconnect->reconnecting (many times possible)->connecting-> Reconnect->connect.
From:http://www.cnblogs.com/xiezhengcai/p/3956401.html
Socket.io, System API,