0

I want to create html5 web multiroom chat, based on HTML5 websocket. But I need some little help to start.

I want to do server side code in c#, but I can not find any tutorials how to do chat websocket server with multi room in c#.

Is there any server which is already implemented in .net, or which I can update to multi room chat ?

It is a little project, one room for 10 peoples. Could you help to me how to start ?

Thank you very much !

I prepare example code structure:

Main server class:

 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(); } } 

Course class: (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; } } 

User class:

 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; } } 

It is good structure for my purpose ? I have many courses (room), with one teacher, in one course can be 20 pupils example .. In one course the pupils can talk with techer using chat (web socket) and drawing board ..

6
  • stackoverflow.com/questions/4985629/… Commented Dec 10, 2012 at 12:09
  • alchemywebsockets.net Commented Dec 10, 2012 at 12:16
  • Yes alchemywebsockets, looks well. It also have sample of chat app, and What if I want to do multi room chat ? What will I need to update ? Thnak you ! Commented Dec 10, 2012 at 12:26
  • Don't expect us to write the code for you. Work on it and if you get stuck somewhere stackoverflow is here to help. Commented Dec 10, 2012 at 12:39
  • I´m sorry, I don´t want from you code .. Me english is not good, so it maybe look as you write .. But I only want the idea, how to solve this problem, because I don´t know where I must start programing .. I search topics about his problem but I don´t find any good solution how to do this think. So if you can help me theoretically what i will need to start with multi room chat, i will be very gratefull. Commented Dec 10, 2012 at 12:54

2 Answers 2

1

That's how I would build the object hierarchy:

The chat server should have a list of ChatRooms.

Each ChatRoom should have a list of ChatUsers.

Each ChatUser should have one or no ChatRoom and an outbound socket.

(this assumes that a user is only in one room at a time. Allowing multiple rooms would make things a bit more complex)

That's how room selection could work:

When a client connects, a ChatUser is created. The first thing the server does is send the list of chatrooms to the user. The client then responds with the name of the chatroom it wants to join. When a chatroom of that name doesn't exist, it is created and added to the global list of rooms.

The client is then added to the room, and the room is set on the client.

That's how chatting could work:

When the socket of the user receives a chat message, it should call a SendToAllClients method on the room the user is currently in (when the room is null, it should return an error message to the user that they must join a room first).

The SendToAll method of the room should then call a SendToClient of all users which are on its list of users.

The SendToClient method of the class would then send the chat message to the client.

How to expand this for multiple chatrooms per user

To allow a client to join multiple chatrooms at once and have separate conversations in them, the client must be able to:

  • request a list of rooms at any time, not just at startup
  • join rooms at any time, not just at startup
  • leave rooms
  • specify the room when sending a message

That means that the action the client wants to perform can not be deduced from the state it is currently in. You need to add this information to the messages of the user. You could, for example, do this as prefixes. Like

!list 

requests the list of rooms

!join:asdf 

join/create the room asdf

_asdf:Hello 

sends the message Hello to the room asdf.

The messages from the server should have similar prefixes, so that the client can deduce if a message is a room list or a chat message and from what room it originates.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this is what I need. Thank you very much Philipp. About clients, my idea is, create techer app, where lector have own class. In the class is about 20 people, lector will have one board (HTML5 canvas) and chat for current class. It is question what will be better to do that client can be at two lessons at one time ? I think that it don´t make sense. So he will need to log off current lesson. So one client can Access only one lesson at one time. Do you think that is good solution ?
So for me chat room is a lesson where teacher have a board, which uses websocket to share board with pupils(clients), and every lesson have possibility chat. DO you think that Alchemy Websockets server is good base for my server side architecture ?
Regarding Alchemy: I have no idea, I never used it.
0

You should try to look into SignalR for ASP.NET (example : jabbr.net/). This may be more helpful and handy.

1 Comment

the link is dead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.