SOCKET.IO use front and rear ends

1. server

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

Listen for client connections, the callback function will pass this socket connection

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

Broadcast messages to all clients

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

To the specified client sends a message

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

Monitor information sent by the client

socket.emit('String', data);

The client sends a message to the socket

 

Broadcast news

// to the client in addition to their broadcast messages 
socket.broadcast.emit ( "MSG", {Data: "Hello, the Everyone"}); 
// a broadcast message to all clients 
io.sockets.emit ( "msg", {data: "hello, all" });

 

Packet

socket.on('group1', function (data) {
        socket.join('group1');
});
socket.on('group2',function(data){
        socket.join('group2');
 });

The client sends

socket.emit ( 'group1'), can be added group1 packet;
socket.emit ( 'group2'), can be added group2 packet;

A client may present a plurality of packets (subscription mode)

Kicked out of the group

socket.leave(data.room);

Transmitting user information packets

// do not include their 
socket.broadcast.to ( 'named group1') EMIT ( 'EVENT_NAME', Data);. 
// including their 
io.sockets.in ( 'group1') emit ( 'event_name', data).;

The method allows the current broadcast is not the inner socket client packet

Get the client socket connections 

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

 

Get group information

// Get all the rooms (packet) information 
io.sockets.manager.rooms 
// get this socketid into the room information 
io.sockets.manager.roomClients [socket.id] 
// get the client in the particular room, return all in this example the room socket 
io.sockets.clients ( 'particular room')

 

Another grouping

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);
})

The client is linked to ws: //103.31.201.154: 5555 but the server can io.of ( '/ some') to filter it out.

 

Further, Socket.IO configuration provides four API: io.configure, io.set, io.enable, io.disable. Wherein the set of the individual io.set, io.enable io.disable a single setting, and Boolean configuration. io.configure allows you to configure different parameters for different production environments (such as devlopment, test, etc.).

2. Client

To establish a socket connection

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

Monitor service message

socket.on ( 'MSG', function (Data) { 
    socket.emit ( 'MSG', {RP: "Fine, Thank you"}); // send a message to the server 
    the console.log (Data); 
});

socket.on ( "String", function (data)) Sting same message service parameters sent by the server side listening emit first parameter

 

Disconnect and reconnect listening socket.

Copy the code
socket.on ( 'the disconnect', function () { 
    the console.log ( "service disconnect"); 
}); 


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

 

Client socket.on () event listeners:

connect: connect successfully
connecting: Connecting
disconnect: Disconnect
connect_failed: Connection failed
error: error occurred, and the event can not be handled by other types of
message: message with a server-side event
anything: anything with a server-side event
reconnect_failed: reconnection failed
reconnect : successful reconnection
reconnecting:'re reconnection
when first connected, event-triggered order: connecting-> connect; when the connection is lost, the event triggering the order: disconnect-> reconnecting (possibly several times) -> connecting- > reconnect-> connect.

Knowledge is known to us is unknown to us based on our existing knowledge to discover the unknown result, the expansion of knowledge is more knowledge we gain knowledge of the unknown will be more so, never-ending expansion of knowledge

1. server

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

Listen for client connections, the callback function will pass this socket connection

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

Broadcast messages to all clients

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

To the specified client sends a message

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

Monitor information sent by the client

socket.emit('String', data);

The client sends a message to the socket

 

Broadcast news

// to the client in addition to their broadcast messages 
socket.broadcast.emit ( "MSG", {Data: "Hello, the Everyone"}); 
// a broadcast message to all clients 
io.sockets.emit ( "msg", {data: "hello, all" });

 

Packet

socket.on('group1', function (data) {
        socket.join('group1');
});
socket.on('group2',function(data){
        socket.join('group2');
 });

The client sends

socket.emit ( 'group1'), can be added group1 packet;
socket.emit ( 'group2'), can be added group2 packet;

A client may present a plurality of packets (subscription mode)

Kicked out of the group

socket.leave(data.room);

Transmitting user information packets

// do not include their 
socket.broadcast.to ( 'named group1') EMIT ( 'EVENT_NAME', Data);. 
// including their 
io.sockets.in ( 'group1') emit ( 'event_name', data).;

The method allows the current broadcast is not the inner socket client packet

Get the client socket connections 

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

 

Get group information

// Get all the rooms (packet) information 
io.sockets.manager.rooms 
// get this socketid into the room information 
io.sockets.manager.roomClients [socket.id] 
// get the client in the particular room, return all in this example the room socket 
io.sockets.clients ( 'particular room')

 

Another grouping

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);
})

The client is linked to ws: //103.31.201.154: 5555 but the server can io.of ( '/ some') to filter it out.

 

Further, Socket.IO configuration provides four API: io.configure, io.set, io.enable, io.disable. Wherein the set of the individual io.set, io.enable io.disable a single setting, and Boolean configuration. io.configure allows you to configure different parameters for different production environments (such as devlopment, test, etc.).

2. Client

To establish a socket connection

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

Monitor service message

socket.on ( 'MSG', function (Data) { 
    socket.emit ( 'MSG', {RP: "Fine, Thank you"}); // send a message to the server 
    the console.log (Data); 
});

socket.on ( "String", function (data)) Sting same message service parameters sent by the server side listening emit first parameter

 

Disconnect and reconnect listening socket.

Copy the code
socket.on ( 'the disconnect', function () { 
    the console.log ( "service disconnect"); 
}); 


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

 

Client socket.on () event listeners:

connect: connect successfully
connecting: Connecting
disconnect: Disconnect
connect_failed: Connection failed
error: error occurred, and the event can not be handled by other types of
message: message with a server-side event
anything: anything with a server-side event
reconnect_failed: reconnection failed
reconnect : successful reconnection
reconnecting:'re reconnection
when first connected, event-triggered order: connecting-> connect; when the connection is lost, the event triggering the order: disconnect-> reconnecting (possibly several times) -> connecting- > reconnect-> connect.

Guess you like

Origin www.cnblogs.com/suneil/p/11287931.html