zoukankan      html  css  js  c++  java
  • websocket

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    
    namespace Fleck.Samples.ConsoleApp
    {
        class Server
        {
            static void Main()
            {
                FleckLog.Level = LogLevel.Debug;
                var allSockets = new List<IWebSocketConnection>();
                var server = new WebSocketServer("ws://10.10.10.99:50000");
                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();
                }
    
            }
        }
    }

    测试方法1:打开浏览器,F12进入调试模式,在Console中输入测试代码。(我使用的是谷歌浏览器)

    ws = new WebSocket("ws://10.10.10.99:50001");
    ws.onopen = function() { 
        ws.send('websocekt测试'); 
    };
    ws.onmessage = function(e) {
        alert("收到服务端的消息:" + e.data);
    };

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

    转载自:https://www.cnblogs.com/swjian/p/10553689.html
  • 相关阅读:
    String 总结
    android 调试源码
    Java 随机数总结
    Android中如何控制调节屏幕亮度
    Activity中通过Bundle传递自定义数据类型
    Android AsyncTask简单用法
    WCF 第五章 可信赖会话
    WCF 第五章 会话级别的实例
    WCF 第五章 行为 在WCF一个服务内部的事务操作
    WCF 第五章 导出并发布元数据(服务行为)
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/14880049.html
Copyright © 2011-2022 走看看