Socket.io long connection based communication protocol - the interactive module

Socket.io long connection based communication protocol - the interactive module

1, the front end projects react client installation module


npm/cnpm i socket.io-client –S

2, services

Currently Solution: Start as a service-based chat dome node of
the latter solution: the development of Java-based version of the service socket.io

3, the package

  • 3.1 socket.js 、 socket-common.js

  • Content 3.2 socket-common.js


    Define the main variable name
    export const C_IMEVT_COMMON $ DATA = '2 ';

    /
    Issued by the client - Protocol Type: Send general data (Note: This event is sure to remain the same as defined in the server-type.js Protocal!)
    /

    export const S_IMEVT_DUPLICATED = 's_evt.duplicated';

    / **
    issued by the server - Protocol Type: repeat the landing was kicked message (Note: This event is sure to remain the same as defined in the server-type.js Protocal!)
    ** /

    export const S_IMEVT_ILLEGAL = 's_evt.illegal';

    / **
    issued by the server - Protocol Type: Illegal Connection refused service event (ie, event notification service will not before the socket with legitimate authentication information sent kicked off, to prevent illegal connections and attack)
    (Note: This event is sure to to maintain and server defined in the protocal-type.js consistent!)
    ** /

  • 3.3 socket.js content


    Mainly some method invocation (there is no way to determine the integrated login permission)

    1, _socket // instance of an object

    2, callback_showChatMessage // success callback function

    3, Socket.initConnect connection initialization method

    4, Socket.sendData message sent by websocket

    5, Socket.disconnectSocket client interrupts the client socket connection

4, initConnect in the method


Initiate the first connection and authentication
this._socket = SocketIOClient.connect (wsUrl, {
Query: 'token =' + JSON.stringify (the Data),
forceNew: to true, // add this option in order to let the client socketio.disconnect () entered into force !
? Secure: supportSSL to true: false // whether to support SSL / TLS
});
callback // when success
this._socket.on ( 'Connect', function () {
that.logInfo ( '[E] this client Socket connect event has triggered ', to true)
});
callbacks when disconnected //
this._socket.on (' the disconnect ', function (Data) {// Data Content iS "IO Server the disconnect"
that.logInfo (' [E ] of this client Socket disconnect event has triggered [eND] ', to true)!
});
error // reconnection
this._socket.on (' connect_error ', function (Data) {
that.logInfo (' [E] the present The client connect_error event triggered '+ JSON.stringify (data), true );
});
// timeout
this._socket.on ( 'connect_timeout', function () {
that.logInfo ( '[E] The present connect_timeout client event triggered', to true);
});
this._socket.on ( 'error', function (ERR ) {
that.logInfo ( '[E] The present client error trigger event has' + ERR, to true);
});
// connection timeout trigger event
this._socket.on ( 'Reconnect', function () {
that.logInfo ( '[E] The present client reconnect event has triggered', to true);
});
// try to reconnect continuous
this._socket.on ( 'reconnect_attempt', function () {
the console.log (the this)
that.logInfo ( '[E] The present client reconnect_attempt event triggered', to true);
});
this._socket.on ( 'reconnect_failed', function () {
that.logInfo ( '[E] The present client reconnect_failed event triggered' , to true);
});
// take this approach if a fault occurs
this._socket.on ( 'reconnect_error', function () {
that.logInfo ( '[E] The present client reconnect_error event triggered', to true);
});
// heartbeat
// alive for verification
// Make
this. _socket.on ( 'of ping', function () {
that.logInfo ( '[E] heartbeat request has been issued →', to true);
});
// received
this._socket.on ( 'pong', function ( ) {
that.logInfo ( '[E] heartbeat response has been received ←', to true);
});
// listening C_IMEVT_COMMON $ DATA message type
this._socket.on (C_IMEVT_COMMON $ DATA, function (P) {
that.logInfo ( 'receive the message:' + JSON.stringify (p), true);
message notification application layer // displays the received
that.callback_showChatMessage (P);
});

5, for use in the project react

    1, in the assembly import Socket from '../common/socket' // write their own path 
    2, initConnect and write callbacks componentDidMount life cycle 
    // For example: 
    Socket.initConnect ( 'http://192.168.7.4: 3000 ', {loginToken: "47384h3uhjfh3j4j343j434h44", 
    loginUserId: "[email protected]"}, to true) 

    // write the callback, the callback to socket.js callback_showChatMessage property assignment 

    Socket.callback_showChatMessage = function (Data) { 
        IF (Data. dataContent) { 
            the let the JSON.parse the Obj = (data.dataContent) 
        } 
    }

Guess you like

Origin www.cnblogs.com/zkqbk/p/11389854.html