简体   繁体   English

WebSockets-如何创建不同的消息?

[英]WebSockets - How to create different messages?

I am creating a websocket chat application and I managed to relay chat messages to other browsers connected. 我正在创建一个websocket聊天应用程序,并且设法将聊天消息中继到连接的其他浏览器。 I have a console application listening on one port. 我有一个控制台应用程序在一个端口上侦听。

My question is... If one person logs on to the system I want everybody to know that, how can I do that? 我的问题是...如果一个人登录到系统,我希望所有人都知道,我该怎么做? I'm using Linq to map the DB but if the logging is ok how do I send that message, that user X has logged in? 我正在使用Linq映射数据库,但是如果日志记录正常,我如何发送该消息,则该用户X已登录?

FINALLY I was able to create a chatroom using websockets, here is the final product, thanks for the orientation! 最终,我能够使用websockets创建聊天室,这是最终产品,感谢方向!

http://ukchatpoint.no-ip.org/Chatpoint/Pages/Uklobby.aspx http://ukchatpoint.no-ip.org/Chatpoint/Pages/Uklobby.aspx

First make sure you're sending messages as JSON (JavaScript Object Notation) as this allows structured data to be sent back and forth, and client & server can differentiate between a chat message and an instruction (eg someone new logged in). 首先,请确保您以JSON(JavaScript对象表示法)的形式发送消息,因为这允许来回发送结构化数据,并且客户端和服务器可以区分聊天消息和指令(例如,新登录的人)。 For instance on the client: 例如在客户端上:

mySocket.onmessage = function(event) {
    var command = JSON.parse(event.data);

    if(command.type === 'message') {
        var message = command.message;
        // handle chat message
    }
    else if (command.type === 'newUser') {
        var username = command.username;
        // handle new user
    }
};

On the server in ASP.NET C# you'd send them as: 在ASP.NET C#的服务器上,您将其发送为:

public class ChatHandler : WebSocketHandler
{
    private JavaScriptSerializer serializer = new JavaScriptSerializer();
    private static WebSocketCollection chatapp = new WebSocketCollection();

    public override void OnMessage(string message)
    {
        var m = serializer.Deserialize<Message>(message);

        switch (m.Type)
        {
            case MessageType.NewUser:
                chatapp.Broadcast(serializer.Serialize(new
                {
                    type = "newUser",
                    username = m.username
                }));

                break;
            case MessageType.Message:
                chatapp.Broadcast(serializer.Serialize(new
                {
                    type = "message",
                    message = m.message
                }));

                break;
            default:
                return;
        }
    }
}

As Hightechrider says, you'll need to keep track of a list of connected clients, that's what WebSocketCollection class does in the code listing above. 就像Hightechrider所说的那样,您需要跟踪已连接客户端的列表,这就是WebSocketCollection类在上面的代码清单中所做的。

Check out Paul Batum's WebSocket chat example on github here ( https://github.com/paulbatum/BUILD-2011-WebSocket-Chat-Samples/blob/master/BasicAspNetChat/ChatHandler.cs ) 在github上查看Paul Batum的WebSocket聊天示例( https://github.com/paulbatum/BUILD-2011-WebSocket-Chat-Samples/blob/master/BasicAspNetChat/ChatHandler.cs

Also he did a presentation at the recent MS BUILD conference here ( http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-807T ) 此外,他还在最近的MS BUILD会议( http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-807T )上做了演讲。

You would need to track the connections at the application level so you can send to all of them. 您需要在应用程序级别跟踪连接,以便可以发送给所有连接。 But take a look at SignalR instead where a lot of the work involved with webSockets and long polling is being written for you. 但是,请看一下SignalR ,那里正在为您编写许多与WebSocket和长时间轮询有关的工作。 With SignalR you can use GetClients to get all the clients connected to a Hub . 使用SignalR,您可以使用GetClients来将所有客户端连接到Hub

使用PostgreSQL时,可以在数据库中使用NOTIFY来通知应用程序层,该应用程序层可以生成通过WebSockets发送的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM