识别Web套接字客户端

时间:2017-02-19 18:48:06

标签: javascript c# php websocket

我想知道是否有人可以帮我解决一个小问题,我试图用.NET中的Fleck Web套接字库和PHP来识别每个客户端

PHP:     

if (isset($_POST['web_socket_username_submit'])) {
    $_SESSION['web_socket_username'] = $_POST['web_socket_username'];
    header("location: ");
}

if (!isset($_SESSION['web_socket_username'])) {
    echo 'You need to select a username before continuing, this username will stick with you untill you clean your cache.<br>';
    echo '<form name="webSocketUsernameForm" method="post">';
    echo '';
    echo '<input type="text" name="web_socket_username" placeholder="Username...">';
    echo '<input type="submit" name="web_socket_username_submit" value="Continue">';
    echo '</form>';
    exit();
}
?>
<!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 += "Emulator: " + evt.data + '<br/>';
            };
            // when the connection is established, this method is called
            ws.onopen = function () {
                inc.innerHTML += 'Emulator accepted your connection.<br/>';
            };
            // when the connection is closed, this method is called
            ws.onclose = function () {
                inc.innerHTML += 'Emulator closed the connection.<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>

C#:

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

namespace WebSockets_Testing
{
    using Fleck;

    class Program
    {
        static void Main(string[] args)
        {
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new List<IWebSocketConnection>();
            var server = new WebSocketServer("ws://127.0.0.1:8181");
            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("You: " + message));
                };
            });


            var input = Console.ReadLine();
            while (input != "exit")
            {
                foreach (var socket in allSockets.ToList())
                {
                    socket.Send(input);
                }
                input = Console.ReadLine();
            }
        }
    }
}

如果客户不被欺骗,我如何识别客户?

1 个答案:

答案 0 :(得分:0)

实现此目的的一种简单方法是使用socket.ConnectionInfo.Id,它为每个套接字返回一个唯一的GUID。