Programing HTML5 websocket chat with rooms - c#

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 ..

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.

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

Related

SignalR - Pushing notification to servers

When I try to broadcast a message to all clients, I can trigger client's javascript code from server and get the job done.
But this time my aim is to trigger a method in all servers. For example, when roles of a user changed in one server, I want to warn other servers about this operation and I want to make other servers retrieve updated user role list for particular user.
Is it possible to do this with SignalR? Can a server behave like a client (browser)?
Yes you can do that.
Let's say you have the following hub:
public class TheHub : Hub
{
public void RoleChanged(int userId)
{
Clients.All.roleChanged(userId);
}
}
On all the listening servers, you'd have to do:
var _connection = new HubConnection("http://localhost:1234/signalr");
var _theHub = _connection.CreateHubProxy("TheHub");
_myHub.On<int>("RoleChanged", userId =>
{
System.Diagnostics.Debug.WriteLine("Changed user's Id: " + userId);
});
_connection.Start().Wait();
To invoke the RoleChanged event, do:
_myHub.Invoke("RoleChanged").Wait();

Msmq what is the right way of implementation of users

So, this is not the right title probably, i was not sure how to put it and how to explain in, lets try !
i have a system that manage more then 720,000 users, i did not use queue until now and i want to start using it, lets give an example os a class that hold a user
[Serializable]
public class UserName
{
public string UserNameName { get; set; }
public string TheResonILikeTo { get; set; }
}
so, if i want to put a message inside my queue its simple
public static void Createandsendmessageclass()
{
//From Windows Service, use this code
MessageQueue messageQueue = null;
if (MessageQueue.Exists(#".\Private$\SomeTestName"))
{
messageQueue = new MessageQueue(#".\Private$\SomeTestName");
messageQueue.Label = "Testing Queue";
}
else
{
// Create the Queue
MessageQueue.Create(#".\Private$\SomeTestName");
messageQueue = new MessageQueue(#".\Private$\SomeTestName");
messageQueue.Label = "Newly Created Queue";
}
UserName user = new UserName();
user.UserNameName = "Alon";
user.TheResonILikeTo = "Fuc??";
messageQueue.Send(user);
}
until here all is great and smooth. nothing to worry about.
But, lets say i have 720,000 users that has messages with different name,
Alon
Erez
*Ylan
and so on..
should i put a different queue for each user, or can i ask the system to give me only messages for user "alon" by usernamename?
another question, how can i tell my code to keep getting messages? running in a while loop?
thanks everyone ! good day
Most important thing to remember is that MSMQ is a network protocol, not a database. It isn't appropriate for a system designed to store messages for 720,000 users.
All your data for users should be stored in a database.

SignalR chat room with Acknowledgement of message received after saving it to Sql data Base

Hello every one I am new to signalR I need little help to follow right approach in my chat module .
My Refernce :
http://www.codeproject.com/Articles/562023/Asp-Net-SignalR-Chat-Room but this article does not uses dataBase.
Steps which I have used to build my chat module are :
1 - Created chatHub class inherited from Hub class.
2 - On Connect(string enryptedId) function in chatHub class I am adding to List<UserDetail>.
3 - On SendPrivateMessage(string toData) I am saving it to database if it saved successfully to dataBase without any exception then sending it to both sender and reciever and binding on their communication messages <div>.
Problem in this approach - If after saving it to database if sender got disconnected due to network problem then sender will not recieve message from chathub class to client function so message is not appended to <div> which shows user communications but actually message sent successfully. Please can any one tell me the right approach to do this.
I would do it something like this, keep in mind its Pseduo code not tested or anything
private Dictionary<Guid, TaskCompletionSource<bool>> transactions = new Dictionary<Guid, TaskCompletionSource<bool>>();
public Task SendPrivateMessage(string content)
{
var taskCompletion = new TaskCompletionSource<bool>();
var transactionId = Guid.NewGuid();
transactions[transactionId] = taskCompletion;
var message = new Message
{
TransactionId = transactionId,
Content = content
};
GlobalHost
.ConnectionManager
.GetHubContext<MyHub>()
.Clients
.Client(connectionId)
.OnMessage(message);
return taskCompletion.Task;
}
public void OnTransactionConfirmed(Guid transactionId)
{
var taskCompletion = transactions[transactionId];
transactions.Remove(transactionId);
taskCompletion.SetResult(true);
}
Message is just a DTO
public class Message
{
public Guid TransactionId { get; set; }
public string Content { get; set; }
}
Basically you send a message to the client using the client method called OnMessage then you wait asynchronously until client confirms with the Hub method called OnTransactionConfirmed
Using a task makes it easier to work asynchronous in a synchronous way, the consumer of SendPrivateMessage could be asynchronous itself (If its called from a WebApi method or similar).
public Task MyWebApiMethod()
{
return myChatLogic.SendPrivateMessage("foo");
}
Or in a synchronous manner
public void MySyncMethod()
{
myChatLogic.SendPrivateMessage("foo").Wait(); //Thread will wait here until client asnwers
}
Note
To make this code failsafe you need to timeout the wait for confirmation and complete the task and remove the transactionId etc
your send message, receive message and display message should be handled separately. you did well with the send message except that you need to add a datetime stamp generated by the Server (do not use time from client) to each message and store it in sql. whenever there is a new message received, the Server should send this message to all online senders' and receivers' client browser or app.
here I said senderS because user A may login using a phone and at the same time login using office desktop browser, if user A send a message to user B mobile phone using the desktop browser, this message should be appear in 3 client device: user A desktop browser, user A mobile phone and user B mobile phone.
for the receiver, whenever go from offline to online, check the last message datetime stored in client side using cookie or file or clientside sqlite or clientside internal storage, with the Server database and get all related messages since last offline by comparing the last message datetime from clientside and message dt from server database.
for the display message, only read from client side storage so that the apps can run offline.
When sending a message, append a random string of 8 characters to the start of the message. On your message received function, cut out the first 8 characters and send those back to the original sender.
If you receive the same characters back from the receiver, then you know that receiver has received the message and has not been disconnected.
You can use this method to generate a random string.
private static IEnumerable<string> RandomStrings(
string allowedChars,
int minLength,
int maxLength,
Random rng)
{
char[] chars = new char[maxLength];
int setLength = allowedChars.Length;
int length = rng.Next(minLength, maxLength + 1);
for (int i = 0; i < length; ++i)
{
chars[i] = allowedChars[rng.Next(setLength)];
}
return new string(chars, 0, length);
}
you will can call the method like this
private string GetRandomStrings(){
const string AllowedChars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz##$^*()";
Random rng = new Random();
return RandomStrings(AllowedChars, 8, 8, 1, rng)
}

Pushing new messages with SignalR and SqlDependency

I have a large table containing items that generally has a few new items inserted every second. I want to build a realtime application where users can subscribe to certain types of items and have a realtime feed whenever an item of a certain type is inserted.
As a start, I used
http://techbrij.com/database-change-notifications-asp-net-signalr-sqldependency but it is missing some crucial pieces that I am not sure how to implement.
What is the best way to track which messages to push to which clients? How can I track which messages have already been sent without the overhead of updating a "pushed" flag every time an update is received?
You can use an open source realization of the SqlDependency class - SqlDependencyEx. It uses a database trigger and native Service Broker notification to receive events about the table changes. The test project for this component does exactly the same functionality what you are looking for. This is an usage example:
int changesReceived = 0;
using (SqlDependencyEx sqlDependency = new SqlDependencyEx(
TEST_CONNECTION_STRING, TEST_DATABASE_NAME, TEST_TABLE_NAME))
{
sqlDependency.TableChanged += (o, e) => changesReceived++;
sqlDependency.Start();
// Make table changes.
MakeTableInsertDeleteChanges(changesCount);
// Wait a little bit to receive all changes.
Thread.Sleep(1000);
}
Assert.AreEqual(changesCount, changesReceived);
With SqlDependecyEx you are able to monitor INSERT, DELETE, UPDATE separately and receive actual changed data (xml) in the event args object. Hope this help.
SignalR provides a Context.ConnectionId in the hub. You can use this as a key in a collection to track which items have been sent.
Here's an example from a logging chat application I wrote:
public class PushNotificationHub : Hub {
public void Send(string name, string message) {
ChatService.ChatUsers[Context.ConnectionId].AddMessage(message);
Clients.All.addMessage(name, message);
}
public override Task OnConnected() {
ChatService.Current.LogMessage($"Client connected: {Context.ConnectionId}");
ChatService.ChatUsers.Add(Context.ConnectionId, new Common.ChatUser(Context.QueryString["name"]));
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled) {
//Application.Current.Dispatcher.Invoke(() => ((MainWindow)Application.Current.MainWindow).LogMessage($"Client disconnected: {Context.ConnectionId}"));
ChatService.Current.LogMessage($"Client disconnected: {Context.ConnectionId}");
ChatService.Current.LogMessage(ChatService.ChatUsers[Context.ConnectionId].PrintLog());
return base.OnDisconnected(stopCalled);
}
}

How can i get connected user id when using signleR

I'm trying to get the connected user id in my signleR application but I didn't manage to find it. Is there any way to get it because I want when two users are chatting just the two of them will receive each other's messages and not the whole group they are connected to, or all clients connected to signleR hub. My main goal is to send private messages between two connected clients.
My code is so complicated but all I have found is how to broadcast my message to all the clients or to a specific group, but I couldn't manage to send a private message. I did this to send user typing message:
public void UserTyping(string msg, int ToClient) { // display User is Typing Msg
Clients.Others.OtherUserIsTyping(msg, ToClient);
}
ClientSide:
var keyPressCount = 0;
$("#<%=MsgToSend.ClientID %>").keypress(function () {
// Throttle the server call with some logic
// Don't want to call the server on every keypress
var ToClientID = $("#ToClientID").val();
if (keyPressCount++ % 10 == 0) {
chat.server.userTyping("Is typing...", ToClientID); // call the server side function
}
});
chat.client.OtherUserIsTyping = function (msg, ToClient) { // declare the function on the chat hub so the server can invoke it
$("#UserTyping").html(msg);
}

Categories