C# SuperSocket 訊息推送

來源:互聯網
上載者:User

標籤:display   input   foreach   exce   public   setup   image   write   nec   

 服務端使用Nuget引用SuperSocket.WebSocket和SuperSocket.Engine

 

伺服器端代碼【控制台】

using SuperSocket.WebSocket;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace Server{    class Program    {        static void Main(string[] args)        {            WebSocketServer server = new WebSocketServer();            server.NewSessionConnected += server_NewSessionConnected;            server.NewMessageReceived += server_NewMessageReceived;            server.SessionClosed += server_SessionClosed;            try            {                server.Setup("127.0.0.1", 40001);//設定連接埠                server.Start();//開啟監聽                PushMsg();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            Console.ReadKey();        }        static void PushMsg()        {            Thread.Sleep(1000);            try { sessionList.ForEach(o => { o.Send("測試訊息推送"); }); }            catch (Exception ex) { }            PushMsg();        }        public static List<WebSocketSession> sessionList = new List<WebSocketSession>();        static void server_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)        {            sessionList.Remove(session);            Console.WriteLine(session.Origin);        }        static void server_NewMessageReceived(WebSocketSession session, string value)        {            Console.WriteLine(value);            session.Send(value);        }        static void server_NewSessionConnected(WebSocketSession session)        {            sessionList.Add(session);            Console.WriteLine(session.Origin);        }    }}
View Code

用戶端代碼【網站】

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title></head><body>    <textarea id="textarea" style="height: 500px; width: 300px;"></textarea>    <input type="button" id="send" onclick="send()" value="發送">    <input type="text" id="message">    <script type="text/javascript">        //檢查瀏覽器是否支援WebSocket        if (!window.WebSocket) {            console.log(‘您的瀏覽器不支援WebSocket,請選擇其他的瀏覽器再嘗試串連伺服器‘);        }        var el = document.getElementById("textarea");        var wsClient = new WebSocket(‘ws://localhost:40001‘);        wsClient.open = function (e) {            el.value += "串連成功!\r\n";        }        wsClient.onclose = function (e) {            el.value += "串連斷開!\r\n";        }        wsClient.onmessage = function (e) {            el.value += "接收訊息:" + e.data + "\r\n";        }        wsClient.onerror = function (e) {            el.value += "串連失敗!原因【" + e.data + "】\r\n";        }        function send() {            var oText = document.getElementById("message");            wsClient.send(oText.value);        }    </script></body></html>
View Code

程式目錄結構

 

 效果

 

C# SuperSocket 訊息推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.