简体   繁体   English

将连接到现有 websocket 的 js 代码转换为 react js 应用程序

[英]convert js code to connect to an existing websocket into a react js app

I have written this js code to connect to my own ruby websocket (two different connections but consider just one. Once connected it waits for a message, parse it and then display a div with some logics (this part is out of scope for now).我已经编写了这个 js 代码来连接到我自己的 ruby​​ websocket(两个不同的连接,但只考虑一个。一旦连接,它会等待一条消息,解析它,然后显示一个带有一些逻辑的 div(这部分现在不在范围内) .

   window.onload = function(){
        var documento_da_visionare = '<?php echo $hk; ?>';
        var websocket = new WebSocket('wss://smartscreen.nangapar.com:8443');
        var websocket1 = new WebSocket('wss://smartscreen.nangapar.com:8443');
        var durata = 30;
        websocket.onopen = function(){ 
          //document.title = "VB: " + documento_da_visionare; 
          websocket.send("registra:" + documento_da_visionare);
        } 
        websocket1.onopen = function(){ 
          websocket1.send("registra:allmypage");
        }
        websocket.onmessage = function(evento){
          nome_comando    = evento.data.split(":")[0] 
          valore_comando = evento.data.substr(nome_comando.length + 1); 
          switch (nome_comando){
          //code will continue with the display logics out of scope here

Everything is working fine but I'd like to implement it in React to learn it.一切正常,但我想在 React 中实现它来学习它。 I have read many articles and tutorial but I need a "kick" on the right direction refactoring this code.我已经阅读了很多文章和教程,但我需要在重构这段代码的正确方向上“踢”一下。 Where should I start from?我应该从哪里开始? The most important improvement I'd like to get is to reconnect on disconnection and to display when the client is disconnected.我想获得的最重要的改进是在断开连接时重新连接并在客户端断开连接时显示。 Any hint/help?任何提示/帮助?

You asked couple of questions:你问了几个问题:

1- How to do re-connect when disconnected and notify user when disconnected: 1-如何在断开连接时重新连接并在断开连接时通知用户:

The best is to use socket.io library, you have to use it on both sides, server and client.最好是使用 socket.io 库,你必须在两端,服务器和客户端使用它。 It handles both of these cases for you, but if you insist to use pure websocket, you have to ping/pong between the server and client to know if the connection is alive or not, one side triggers, ping, and the other side answers with pong, and the pong side should check with larger interval of the ping to see if it was pinged or not.它为您处理这两种情况,但是如果您坚持使用纯 websocket,则必须在服务器和客户端之间进行 ping/pong 以了解连接是否存在,一侧触发,ping,另一侧回答用pong,pong侧要检查ping的较大间隔,看是否被ping通。

2- How to do it in React: 2- 如何在 React 中做到这一点:

If you are new to reactjs, the path is a bit long.如果你是 reactjs 的新手,路径有点长。 First learn reactjs in general, and then learn the one-directional data flow in react, and then you can use some state-management like MobX.先大体学习reactjs,再学习react中的单向数据流,然后就可以使用一些状态管理,比如MobX。 When you get an event from server, you update the state, then the state fire re-render event to UI.当您从服务器获取事件时,您更新状态,然后状态将重新渲染事件触发到 UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM