[Switch] SOCKET.IO, system API,

Original: http://www.cnblogs.com/xiezhengcai/p/3956401.html 

1. Server

io.on('connection',function(socket));

Listen to the client connection, the callback function will pass the socket of this connection

io.sockets.emit('String',data);

Broadcast message to all clients

io.sockets.socket(socketid).emit('String', data);

Send a message to the specified client

socket.on('String',function(data));

Listen to the information sent by the client

socket.emit('String', data);

Send a message to the client of the socket

 

broadcast message

// Broadcast messages to clients other than themselves 
socket.broadcast.emit("msg",{data:"hello,everyone" }); 
 // Broadcast messages to all clients 
io.sockets.emit("msg", {data:"hello,all"});

 

grouping

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;

A client can have multiple groups (subscription mode)

kick out of the group

socket.leave(data.room);

Send messages to users in a group

// Does not include its own 
socket.broadcast.to('group1').emit('event_name' , data);
 // includes its own 
io.sockets.in ( 'group1').emit('event_name', data);

The broadcast method allows the current socket client to not be in the group

Get the connected client socket 

io.sockets.clients().forEach(function (socket) {
    //.....
})

 

Get group information

//获取所有房间(分组)信息
io.sockets.manager.rooms
//来获取此socketid进入的房间信息
io.sockets.manager.roomClients[socket.id]
//获取particular room中的客户端,返回所有在此房间的socket实例
io.sockets.clients('particular room')

 

另一种分组方式

io.of('/some').on('connection', function (socket) {
    socket.on('test', function (data) {
        socket.broadcast.emit('event_name',{});
    });
});

客户端

var socket = io.connect('ws://103.31.201.154:5555/some')
socket.on('even_name',function(data){
   console.log(data);
})

客户端都链接到ws://103.31.201.154:5555 但是服务端可以通过io.of('/some')将其过滤出来。

 

另外,Socket.IO提供了4个配置的API:io.configure, io.set, io.enable, io.disable。其中io.set对单项进行设置,io.enable和io.disable用于单项设置布尔型的配置。io.configure可以让你对不同的生产环境(如devlopment,test等等)配置不同的参数。

2. 客户端

建立一个socket连接

var socket = io("ws://103.31.201.154:5555");

监听服务消息

socket.on('msg',function(data){
    socket.emit('msg', {rp:"fine,thank you"}); //向服务器发送消息
    console.log(data);
});

socket.on("String",function(data)) 监听服务端发送的消息 Sting参数与服务端emit第一个参数相同

 

监听socket断开与重连。

copy code
socket.on('disconnect', function() {
    console.log("与服务其断开");
});


socket.on('reconnect', function() {
    console.log( "Reconnect to the server" );
});
copy code

 

Events monitored by client socket.on():

connect: connection successful
connecting: connecting
disconnect: disconnecting
connect_failed: connection failed
error: error occurred and cannot be handled by other event types
message: same as server-side message event
anything: same as server-side anything event
reconnect_failed: reconnect failed
reconnect : Reconnecting successfully
reconnecting: Reconnecting
When connecting for the first time, the event triggering sequence is: connecting->connect; when the connection is lost, the event triggering sequence is: disconnect->reconnecting (may be performed multiple times)->connecting- >reconnect->connect.

Knowledge is what we know and what we don't know. Based on the knowledge we have, we discover the unknown. Thus, the knowledge is expanded. The more knowledge we gain, the more unknown knowledge. Therefore, the expansion of knowledge is endless.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519418&siteId=291194637