【オリジナル】mock websocketリンクの方法


Websocketのデータプッシュ機能をしていますが、mockシミュレーションデータを使用することはできません.nodeがサービスを開始した後、ランダムデータをプッシュすることを考えています.最初のステップでは、インストール依存関係をグローバルにインストールできます.
npm i nodejs-websocket -g
第2ステップは、次のコードcopyをindexに渡す.jsのファイルで
//        ,           ,   tcp.read()  ,      。 
process.on('uncaughtException', function (err) {
  console.log(err);
  console.log(err.stack);
});
try{
    var ws=require("nodejs-websocket")
    var server=ws.createServer(function (conn) {
	    console.log("new connection");
        /**
         *       
         */
        setInterval(()=>{
            conn.sendText("send from server:")
        },2000)
        //               //
        conn.on("text",function (str) {
            console.log("received data:"+str)
            conn.sendText("send from server:"+str)
        })
        //        //
        conn.on("close",function (coded,reason) {
            console.log("connection closed")
        })
        //      //
        conn.on("error", function(code,reason) {
            console.log('    ', code)
        })
    }).listen(8088)
}
catch(e)
{
    console.log("exception:"+e);
}


第3ステップはnodeでindexを実行する.jsファイル、コンソールの下にあるこのログが表示され、websocketが走り出したことを示します.
new connection
ステップ4クライアントのコードに次のコードを記述します.
	  var ws = new WebSocket("ws://localhost:8088/");//        
      ws.onopen=function(){ 			 // onopen     
	        console.log("websocket open");
      ws.onclose=function(){ 			// onclose      
	        console.log("websocket close");
      }
      ws.onmessage =function(e){ 		// onmessage         
		   console.log(”onmessage:“,e.data);
      }

クライアントを実行し、ステップ4を実行すれば
ページを切り替えるときはwebsocketを閉じてください
ws.close()