C # SuperSocket Message push

Source: Internet
Author: User

Service side uses NuGet to refer to Supersocket.websocket and Supersocket.engine

Server-side Code "console"

usingSupersocket.websocket;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceserver{classProgram {Static voidMain (string[] args) {Websocketserver Server=NewWebsocketserver (); Server. Newsessionconnected+=server_newsessionconnected; Server. Newmessagereceived+=server_newmessagereceived; Server. Sessionclosed+=server_sessionclosed; Try{server. Setup ("127.0.0.1",40001);//Setting up PortsServer. Start ();//Turn on monitoringpushmsg (); }            Catch(Exception ex) {Console.WriteLine (ex).            Message);        } console.readkey (); }        Static voidpushmsg () {Thread.Sleep ( +); Try{Sessionlist.foreach (o = = {O.send ("test message Push"); }); }            Catch(Exception ex) {} pushmsg (); }         Public StaticList<websocketsession> sessionlist =NewList<websocketsession>(); Static voidServer_sessionclosed (websocketsession session, SuperSocket.SocketBase.CloseReason value) {sessionl Ist.            Remove (session); Console.WriteLine (session.        Origin); }        Static voidServer_newmessagereceived (websocketsession session,stringvalue)            {Console.WriteLine (value); Session.        Send (value); }        Static voidServer_newsessionconnected (websocketsession session) {Sessionlist.add (session); Console.WriteLine (session.        Origin); }    }}
View Code

Client Code "Web site"

<! DOCTYPE Html>"en"> <meta charset="UTF-8"> <title></title>"textarea"style="height:500px; width:300px;"></textarea> <input type="Button"Id="Send"onclick="Send ()"Value="Send"> <input type="text"Id="message"> <script type="Text/javascript">//Check if the browser supports WebSocket        if(!window. WebSocket) {Console.log ('your browser does not support WebSocket, please select a different browser and try to connect to the server'); }        varEl = document.getElementById ("textarea"); varWSClient =NewWebSocket ('ws://localhost:40001'); Wsclient.open=function (e) {El.value+="Connection Successful! \ r \ n"; } wsclient.onclose=function (e) {El.value+="The connection is broken! \ r \ n"; } wsclient.onmessage=function (e) {El.value+="Receive message:"+ E.data +"\ r \ n"; } wsclient.onerror=function (e) {El.value+="Connection Failed! Reason ""+ E.data +""\ r \ n"; } function Send () {varOtext = document.getElementById ("message");        Wsclient.send (Otext.value); }    </script></body>
View Code

Program directory Structure

Effect

C # SuperSocket Message push

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.