3 Replies - 2070 Views - Last Post: 06 December 2012 - 06:41 AM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Using HTML5 Web Sockets For Private Chatting?

Posted 08 November 2012 - 05:35 AM

So I've just been messing with html5 web sockets and I understand how to send basic messages to the sever and have it output that string (for something like a chat room) but what if you want to use your sockets server to have users private chat one and other? How would I even begin trying to do this or where do you start if anyone has experience with this? thanks guys
Is This A Good Question/Topic? 0
  • +

Replies To: Using HTML5 Web Sockets For Private Chatting?

#2 ChristianS  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 05-December 12

Re: Using HTML5 Web Sockets For Private Chatting?

Posted 05 December 2012 - 12:56 PM

View Postadn258, on 08 November 2012 - 05:35 AM, said:

So I've just been messing with html5 web sockets and I understand how to send basic messages to the sever and have it output that string (for something like a chat room) but what if you want to use your sockets server to have users private chat one and other? How would I even begin trying to do this or where do you start if anyone has experience with this? thanks guys


If your using .net try the SignalR framework, it tries using websockets first than falls back to diverse legacy methods to simulate server push where websockets are unavailable.

SignalR will also solve all the problems you mentioned in a very good way, out of the box so to say.

Similar frameworks should be available for all common server side technologies.

Shalom!
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Using HTML5 Web Sockets For Private Chatting?

Posted 05 December 2012 - 01:21 PM

View Postadn258, on 08 November 2012 - 12:35 PM, said:

... but what if you want to use your sockets server to have users private chat one and other?

You control the sockets server and what messages go where. If you want to have two users chat privately, you simply don't send those messages to anybody except the intended target.

As an example, lets assume you already have code that lets users log into your site and shows you a list of online users. It then lets you select a user from the list and send a private message to that user. The Javascript would send the message and the ID of the intended target user to the server as, for example, a JSON object. The server can then use that to send the message only to that user.

Consider this example code. (It's not written as any particular language, although I kind of had C# in mind when I wrote it. Think of it as C# flavored pseudo code :))
class SocketServer {
    // A list of WebSocket connections.
    private List<Client> clients;
    
    // Executed when a new WebSockets connection is made.
    public function onConnection(Client client) {
        clients.Add(client);
    }
    
    // Executed when a WebSocket connection pushes a message to the server.
    public function onMessage(Client sender, String rawInput) {
        // The MessageData is responsible here for parsing the raw input
        // from the sender and provide the receiver ID and the text message.
        MessageData data = new MessageData(rawInput);
        
        for (Client c in clients) {
            if (c.getID() == data.ReceiverID) {
                c.Send(sender.getID() + ":" + data.Message);
            }
        }
    }
}


Do you see what I mean? It goes through all the connected Client connections, but only sends the message to the connection the user specified. This way, that message will be private.

View PostChristianS, on 05 December 2012 - 07:56 PM, said:

SignalR will also solve all the problems you mentioned in a very good way, out of the box so to say.

How so? I skimmed the docs and it just appears to be a WebSockets base for .NET servers, with some fallback options for old browsers. (Which seem to be popping up all over the place these days.) How does it solve the private chat scenario adn258 was asking about?
Was This Post Helpful? 0
  • +
  • -

#4 ChristianS  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 05-December 12

Re: Using HTML5 Web Sockets For Private Chatting?

Posted 06 December 2012 - 06:41 AM

View PostAtli, on 05 December 2012 - 01:21 PM, said:

How so? I skimmed the docs and it just appears to be a WebSockets base for .NET servers, with some fallback options for old browsers. (Which seem to be popping up all over the place these days.) How does it solve the private chat scenario adn258 was asking about?


SignalR has out of the box solutions for that scenario. Not unlike the once you described. I suggested signalR over using raw websockets as there still are many browsers and servers which do not support them. The server problem is relativly easy to solve the client problem however impossible, as web developers we must always expect legacy browsers on the client side, thats where SignalR comes in very handy. If we on the other hand are talking about an intranett application your solution is valid.

if your looking for a concrete implementation of this on the other hand here is some code which might help, first is sending a message to a specified group (please ignore my private ChatMessage object in here):

        
        /// <summary>
        /// Sends a value to the specified group.
        /// </summary>
        /// <param name="groupName">The name of the group.</param><param name="value">The value to send.</param>
        /// <returns>
        /// A task that represents when send is complete.
        /// </returns>
        public Task Send(string groupName, object value)
        {
            var chatMessage = new ChatMessage(value.ToString(), Context.User.Identity.Name, Context.ConnectionId,
                                              groupName);
            return Clients[groupName].addMessage(chatMessage);
        }


or you could say:
return Clients[Context.ConnectionId]
if you want to send a message to the requesting user or
return Clients[receiverCconnectionId]
where the receiverCconnectionId parameter could be passed in to the method as an argument.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1