zoukankan      html  css  js  c++  java
  • c#WebSocket

    最近需要WebSocket,就去网上找了Demo

    先附地址:https://www.cnblogs.com/sheseido/p/7047948.html

    先是Html端的代码,也算客户端吧:新建一个WebApplication空项目就行了,然后添加要给Html页,设未起始页

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
        <title>websocket client</title>
        <script type="text/javascript">
            var start = function () {
                var inc = document.getElementById('incomming');
                var wsImpl = window.WebSocket || window.MozWebSocket;
                var form = document.getElementById('sendForm');
                var input = document.getElementById('sendText');
    
                inc.innerHTML += "connecting to server ..<br/>";
    
                // create a new websocket and connect
                window.ws = new wsImpl('ws://localhost:7181/');
    
                // when data is comming from the server, this metod is called
                ws.onmessage = function (evt) {
                    inc.innerHTML += evt.data + '<br/>';
                };
    
                // when the connection is established, this method is called
                ws.onopen = function () {
                    inc.innerHTML += '.. connection open<br/>';
                };
    
                // when the connection is closed, this method is called
                ws.onclose = function () {
                    inc.innerHTML += '.. connection closed<br/>';
                }
    
                form.addEventListener('submit', function (e) {
                    e.preventDefault();
                    var val = input.value;
                    ws.send(val);
                    input.value = "";
                });
    
            }
            window.onload = start;
        </script>
    </head>
    <body>
        <form id="sendForm">
            <input id="sendText" placeholder="Text to send" />
        </form>
        <pre id="incomming"></pre>
    </body>
    </html>

    然后就是服务端:一个控制台应用程序即可

    using Fleck;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace websocket
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                FleckLog.Level = LogLevel.Debug;
                var allSockets = new List<IWebSocketConnection>();
                var server = new WebSocketServer("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));
                    };
                });
    
    
                var input = Console.ReadLine();
                while (input != "exit")
                {
                    foreach (var socket in allSockets.ToList())
                    {
                        socket.Send(input);
                    }
                    input = Console.ReadLine();
                }
    
            }
        }
    }

    然后先运行服务端,在运行客户端,就可以相互发送消息了

  • 相关阅读:
    网络营销高人的八点心得/搜索引擎推广方法
    先富者的生活方式,必须注重质量
    知识素养
    成功创业的20条法则, 成为行业中世界顶尖
    ]梦想因人的追逐而变的伟大!
    职场做人既要对事也要对人
    宽恕:管理人的最佳武器
    别了,2006
    让你在职场游刃有余的10句话
    开发客户的十大传世技巧
  • 原文地址:https://www.cnblogs.com/fanlin92/p/13626109.html
Copyright © 2011-2022 走看看