关于C# Fleck和Html5实现WebSocket

关于C# Fleck和Html5实现WebSocket

1、下载Fleck。
https://github.com/statianzo/Fleck

2、把Fleck拖入项目工程。

3、测试代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fleck;

namespace fleck
{
    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:8081");
            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();
            }
        }
    }
}

4、websocket.html

<!DOCTYPE html>
<html>
<head>
    <title>WebSocket</title>

    <style>
        html, body {
            font: normal 1em arial, helvetica;
        }

        #log {
            width: 400px;
            height: 200px;
            border: 1px solid #000000;
            overflow: auto;
        }

        #msg {
            width: 330px;
        }
    </style>

    <script>
        var socket;

        function init() {
      //var host = "ws://127.0.0.1:8080/cts/ctsWebSocket";
            var host = "ws://127.0.0.1:8081/";
            try {
                socket = new WebSocket(host);
                socket.onopen = function (msg) {
                    log("Begin Connection!");
                };
                socket.onmessage = function (msg) {
                    log(msg.data);
                };
                socket.onclose = function (msg) {
                    log("Lose Connection!");
                };
            }
            catch (ex) {
                log(ex);
            }
            $("msg").focus();
        }

        function send() {
            var txt, msg;
            txt = $("msg");
            msg = txt.value;
            if (!msg) {
                alert("Message can not be empty");
                return;
            }
            txt.value = "";
            txt.focus();
            try {
                socket.send(msg);
            } catch (ex) {
                log(ex);
            }
        }

        window.onbeforeunload = function () {
            try {
                socket.send('quit');
                socket.close();
                socket = null;
            }
            catch (ex) {
                log(ex);
            }
        };


        function $(id) {
            return document.getElementById(id);
        }
        function log(msg) {
            $("log").innerHTML += "<br>" + msg;
        }
        function onkey(event) {
            if (event.keyCode == 13) {
                send();
            }
        }
    </script>

</head>
<body onload="init()">
<h3>WebSocket</h3>
<br>
<div id="log"></div>
<input id="msg" type="textbox" onkeypress="onkey(event)"/>
<button onclick="send()">发送</button>
</body>
</html>

 

5、测试

一条评论

  1. 引用通告:websocket与C# socket相互通信_Python技术站

发表回复

您的电子邮箱地址不会被公开。