C # in the main is using Fleck, through NuGet search installation on the line
C # code, console program:
usingFleck;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacews{classProgram {Static voidMain (string[] args) {Flecklog.level=Loglevel.debug; varAllsockets =NewList<iwebsocketconnection>(); varServer =NewWebsocketserver ("ws://0.0.0.0:7181"); Server. Start (Socket={socket. OnOpen= () + ={Console.WriteLine ("open!"); Allsockets.add (socket); }; Socket. OnClose= () + ={Console.WriteLine ("close!"); Allsockets.remove (socket); }; Socket. OnMessage= Message = ={Console.WriteLine (message); Allsockets.tolist (). ForEach (S= S.send ("Echo:"+message)); }; }); varinput =Console.ReadLine (); while(Input! ="Exit") { foreach(varSocketinchallsockets.tolist ()) {socket. Send (input); } Input=Console.ReadLine (); } } }}
In HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"><HTML><Head> <title>WebSocket Client</title> <Scripttype= "Text/javascript"> varStart= function () { varInc=document.getElementById ('incomming'); varWsimpl=window. WebSocket||window. Mozwebsocket; varform=document.getElementById ('Sendform'); varinput=document.getElementById ('SendText'); Inc.innerhtml+= "connecting to server. <br/>"; //Create a new websocket new connection port of 7181window.ws= NewWsimpl ('ws://192.168.40.185:7181/'); //when data is sent from the Server service center, continue to run the process downWs.onmessage= function(evt) {inc.innerhtml+=Evt.data+ '<br/>'; }; //prompt Open When Link object finds successful docking of serverWs.onopen= function() {inc.innerhtml+= '.. connection Open<br/>'; }; //when the linked object does not find the server successfully docking, prompt to open failed, do not cut off the itemWs.onclose= function() {inc.innerhtml+= '.. connection Closed<br/>'; } form.addeventlistener ('Submit', function(e) {e.preventdefault (); varVal=Input.value; Ws.send (Val); Input.value= ""; }); } window.onload=start; </Script></Head><Body> <formID= "Sendform"> <inputID= "SendText"placeholder= "Text to send" /> </form> <PreID= "Incomming"></Pre></Body></HTML>
Use effect:
Original: https://www.cnblogs.com/sheseido/p/7047948.html
C # in WebSocket