associating Custom Data with a connection

Jan 21, 2015 at 10:22 PM
Good Morning,

I have been playing with SuperSocket (which has been working very well) and have a simple setup working that pushes JSON messages between the server and the various clients. Whilst this works well I would like to try and make the system more efficiency and allow users to minimize their data usage.

My scenario is that my server receives pager messages from a decoder, and then pushes all the messages to the clients, and the clients then determine what to do with the specific message (e.g. display it, or ignore it), however I would like to now assign a custom data to each connection so that I can receive the message from the decoder, parse information from it and the server then decide it it should be sent to a specific user or not.

I am working in vb.net and I am wondering if anyone could post an example on how to assign custom data to a connection. A room example would be fine as well but at least it should get me started.

I am pulling data from a MySQL database, so I will look up the user in the database to find which rooms they have subscribed to, and I have also seen the demo below which allows you to send data to the sessions where CompanyID = companyid but what I am not sure about is how to create a CompanyID property so that I can then on a connection being opened assign a CompanyID (e.g. s.companyID = mylookupfunction)

On a side note, can the custom data also be an array or object (e.g. in the room scenario, the user might want to subscribe to multiple rooms, how is the best to handle that?)


Demo I have seen:

var sessions = appServer.GetSessions(s => s.CompanyId == companyId);
foreach(var s in sessions)
{
s.Send(data, 0, data.Length);
}


Many Thanks!
Jan 22, 2015 at 9:51 PM
So if it helps, I have used a code sample and converted it to vb.net, what I am not sure about is in the above demo (which I have converted to vb.net below) is how in my websocket server code below I can create a custom property called CompanyId, so that when a user logs into the web server I can do my lookups or whatever to find the company ID and associate it with the connection. I am just not really sure how to create a custom parameter CompanyID within the webserver /

The second question is, how can I assign something like an array as a custom parameter or is there a better way to do this. My concept is to 'subscribe' users to specific 'rooms' but one user may be subscribed to multiple rooms, so i am a bit unsure how I can use the above example to send data to users where a specific room is in the array?

Many Thanks!

vb.net conversion of the custom parameter:
Dim sessions = ws.GetSessions(Function(s) s.CompanyId = companyId)
For Each s As var In sessions
    s.Send(data, 0, data.Length)
Next
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports SuperWebSocket
Imports SuperSocket
Imports SuperSocket.SocketBase

Namespace SuperWebSocketTest
    Public Partial Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private ws As SuperWebSocket.WebSocketServer

        Private Sub button1_Click(sender As Object, e As EventArgs)
            Dim r As New SuperSocket.SocketBase.Config.RootConfig()

            Dim s As New SuperSocket.SocketBase.Config.ServerConfig()
            s.Name = "SuperWebSocket"
            s.ServiceName = "SuperWebSocket"
            s.Ip = "Any"
            s.Port = 8089
            s.Mode = SocketMode.Async

            If checkBox1.Checked = True Then
                s.Mode = SocketMode.Sync
                s.Security = "tls"

                Dim cert As New SuperSocket.SocketBase.Config.CertificateConfig()

                cert.FilePath = "C:\SSL\yourhostname.cer"
                cert.Password = ""
                cert.IsEnabled = True

                s.Certificate = cert
            End If

            Dim f As New SuperSocket.SocketEngine.SocketServerFactory()


            If ws IsNot Nothing Then
                ws.[Stop]()
                ws = Nothing
            End If

            ws = New WebSocketServer()
            ws.Setup(r, s, f)
            AddHandler ws.NewSessionConnected, AddressOf ws_NewSessionConnected
            AddHandler ws.NewMessageReceived, AddressOf ws_NewMessageReceived
            AddHandler ws.NewDataReceived, AddressOf ws_NewDataReceived
            ws.Start()
        End Sub

        Private Sub ws_NewDataReceived(session As WebSocketSession, e As Byte())
            session.Send(e)
        End Sub

        Private Sub ws_NewMessageReceived(session As WebSocketSession, e As String)
            session.Send(Convert.ToString("Message: ") & e)
        End Sub

        Private Sub ws_NewSessionConnected(session As WebSocketSession)
            session.Send("Hello From C#!")
        End Sub

        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs)
            If ws IsNot Nothing Then
                ws.[Stop]()
                ws = Nothing
            End If
        End Sub
    End Class
End Namespace
Coordinator
Jan 28, 2015 at 2:39 AM
You can define your own session container and put in/out session from/to the container by yourself.