C # in WebSocket

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.