使用房间编写HTML5 websocket聊天

时间:2012-12-10 12:04:26

标签: c# asp.net html5 websocket

我想基于HTML5 websocket创建html5 web多房间聊天。 但我需要一些帮助才能开始。

我想在c#中做服务器端代码,但我找不到任何教程如何在c#中与多个房间聊天websocket服务器。

是否有任何服务器已经在.net中实现,或者我可以更新到多房间聊天?

这是一个小项目,10个人的一个房间。你能帮帮我怎么开始吗?

非常感谢!

我准备了示例代码结构:

主服务器类:

    class Program
{

    // List of courses, which are currentli avalible ( REPRESENT CHAT ROOM) 
    protected static ConcurrentDictionary<Course, string> OnlineUsers = new ConcurrentDictionary<Course, string>();

    static void Main(string[] args)
    {
        // Initialize the server on port 81, accept any IPs, and bind events.
        var aServer = new WebSocketServer(81, IPAddress.Any)
        {
            OnReceive = OnReceive,
            OnSend = OnSend,
            OnConnected = OnConnect,
            OnDisconnect = OnDisconnect,
            TimeOut = new TimeSpan(0, 5, 0)
        };

        aServer.Start();

        // Accept commands on the console and keep it alive
        var command = string.Empty;
        while (command != "exit")
        {
            command = Console.ReadLine();
        }

        aServer.Stop();
    }

    // event when the clients connect to server 
    // Server send to client list of Lessons which are avalible, after 
    private static void OnConnect(UserContext context)
    {
        throw new NotImplementedException();
    }

    // event whent the client, want to disconnect from server
    private static void OnDisconnect(UserContext context)
    {
        throw new NotImplementedException();
    }

    // event, when client is sending some data
    private static void OnSend(UserContext context)
    {
        throw new NotImplementedException();
    }

    // event, when server receive data from client
    // client choose which room want to join and, we add cleint to list of lessons which he choose
    // another method ... Register, Rename, LogOff ...
    private static void OnReceive(UserContext context)
    {
        throw new NotImplementedException();
    }
}

课程类:(ROOMS)

    class Course
{
    // Every course has list of active users
    protected static ConcurrentDictionary<User, string> OnlineUsers = new ConcurrentDictionary<User, string>();

    // Name of course
    public String CourseName { get; set; }

}

用户类:

    class User
{
    // Name of User
    public string Name = String.Empty;

    // UserContext  - Contains data we will export to the Event Delegates. 
    public UserContext Context { get; set; }
}

我的目的是好的结构吗?我有很多课程(房间),一个老师,一个课程可以是20个学生的例子。在一个课程中,学生可以使用聊天(网络插座)和绘图板与techer交谈..

2 个答案:

答案 0 :(得分:1)

我将如何构建对象层次结构:

聊天服务器应该有一个ChatRoom的列表。

每个ChatRoom都应该有一个ChatUser s列表。

每个ChatUser应该有一个或没有ChatRoom和一个出站套接字。

(这假设用户一次只能在一个房间。允许多个房间会让事情变得更复杂)

选择房间的方式如下:

当客户端连接时,会创建ChatUser。服务器做的第一件事就是将聊天室列表发送给用户。然后,客户端以其要加入的聊天室的名称进行响应。当该名称的聊天室不存在时,会创建该聊天室并将其添加到全局房间列表中。

然后将客户端添加到房间,并在客户端上设置房间。

聊天的效果如何:

当用户的套接字收到聊天消息时,它应该在用户当前所在的房间内调用SendToAllClients方法(当房间为空时,它应该向用户返回一条错误消息,告知用户他们必须先加入一个房间。)

会议室的SendToAll方法应该呼叫其用户列表中所有用户的SendToClient

然后,该类的SendToClient方法会将聊天消息发送给客户端。

如何针对每个用户的多个聊天室展开

要允许客户端一次加入多个聊天室并在其中分别进行对话,客户端必须能够:

  • 随时请求房间清单,而不仅仅是在启动时
  • 随时加入会议室,而不仅仅是在启动时
  • 离开房间
  • 在发送消息时指定房间

这意味着客户端要执行的操作无法从当前所处的状态中推断出来。您需要将此信息添加到用户的消息中。例如,您可以将此作为前缀。像

!list

请求房间列表

!join:asdf

加入/创建房间asdf

_asdf:Hello

将消息Hello发送到房间asdf。

来自服务器的消息应该具有类似的前缀,以便客户端可以推断出消息是房间列表还是聊天消息,以及它来自哪个房间。

答案 1 :(得分:0)

您应该尝试查看SignalR for ASP.NET(例如:jabbr.net/)。这可能更有帮助和方便。

相关问题