how react js acts as a websocket client?

前端 未结 3 1682
野的像风
野的像风 2020-12-31 05:44

I want to create a Websocket based client-server Application. In that I\'m created node js websocket server which is waiting for the clients. Now I want to create react js w

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 06:05

    Just create rest program from server side and create the connection at a Web page.

    var connection = new WebSocket('ws://localhost/echo', ['soap', 'xmpp']);
    
    opening the connection 
    connection.onopen = function () {
      connection.send('Ping'); // 
    };
    
    
    connection.onerror = function (error) {
      console.log('WebSocket Error ' + error);
    };
    
    //to receive the message from server
    connection.onmessage = function (e) {
      console.log('Server: ' + e.data);
    };
    
    // Sending String  
    connection.send('your message');
    

    At server side you will get session and message, So you can do communication with N sessions.

提交回复
热议问题