zoukankan      html  css  js  c++  java
  • Fleck websocket官方事例

    server:

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

    namespace ConsoleApplication1
    {
    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:8181");
    server.RestartAfterListenError = true;
    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();
    }
    }
    }
    }

    Client:

    <!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:8181/');
    // 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>

    在谷歌级firefox测试通过。

  • 相关阅读:
    软件测试学习总结
    MySQL数据库中主键和索引的区别和联系
    什么是接口测试及其测试流程
    bug生命周期
    啊这...2-get/post请求区别,来给你看看post请求url中传参
    啊这...1-get/post请求区别,你还在认为get只能在url传参吗?传json格式会咋样?
    关于博客园全站内容审核中...如出现此问题:请移步xxx
    git-2-企业级gitlab的使用及管理工具Sourcetree
    fiddler-12-Proxifier+fiddler进行PC端抓包
    微信小程序弹出订阅消息确认弹窗的限制
  • 原文地址:https://www.cnblogs.com/dacong/p/6380049.html
Copyright © 2011-2022 走看看