0

I have the following C# code using XSockets library:

var client = new XSocketClient("ws://awebsocket.org:4502/Controller", "*");
client.Bind("commands", e => { ... });

Now I'm trying to port this code to Python 2.7 with WebSocket package:

ws = WebSocketApp("ws://awebsocket.org:4502/Controller")

My problem is, that the WebSocket package does not provide such ".Bind()" method. Everything I have is onmessage-, onerror-callbacks. Now I'm thinking of implementing this binding by myself, but I dont really know, how XSockets implemented this internally. Is there any specification about it? Does the XSockets-Server sends this messages over another url like ws://awebsocket.org:4502/Controller/messages or ws://awebsocket.org:4502/Controller?messages ?

DiKorsch
  • 1,240
  • 9
  • 20
  • Probably the best approach would be to install the NuGet package "XSockets.JsApi", and see what the "XSocket.WebSocket" class is doing in "XSockets.latest.js" Javascript is doing. – vtortola Jun 10 '14 at 11:54
  • looks like XSockets is simply based on WebSockets('window.WebSocket') and adds this bind-functionality. Theoretically I would receive all messages via 'onmessage'-callback, wouldnt I? can some of the XSockets gurus verify my thesis? – DiKorsch Jun 11 '14 at 05:33

1 Answers1

1

In XSockets 3.* the framework support pub/sub which means that client subscribe to topics and then get messages when there is a publication on the topic.

So your assumption about all messages being sent to all clients is wrong. It would be a very bad solution to send all messages to all clients and let the client API filter away messages not wanted. So the server actually keeps track of the clients subscribing.

Ofcourse there is much more to it than that in XSockets since you also can choose to only send to a subset of the subscribers by saying for example

this.SendTo(p => p.Age > 18 && p.Age < 25 && p.Location == "UK", "Hello young people in UK", "topicOfChoice");

If you want to build you own client the XSockets team will surely help you out. You actually only need the get the information about what to send to "subscribe", "unsubscribe" etc. All messages follow a protocol that XSockets uses to know where to dispatch them and what to do.

I would strongly recomend to write the client for 4.0 since there are very big changes in 4.0 vs 3.*

In 4.0 a message can look like this

{"D":"{\"text\":\"Hello World\"}","C":"Chat","T":"chatmessage"}

That message basically means:

DATA

"D":"{\"text\":\"Hello World\"}"

CONTROLLER

"C":"Chat"

METHOD/TOPIC

"T":"chatmessage"

So the messages really say, send {text:'Hello World'} to the controller Chat and the method ChatMessage (could be some misstakes, writing from my head)

The controller Chat could look something like this

public class Chat : XSocketController
{
    public void ChatMessage(string text)
    {
        this.InvokeToAll(text,"chatmessage");
    }
} 

The sample above uses RPC which means that all clients connected to this controller would get the message. If you need publish/subscribe you just replace

this.PublishToAll(text,"chatmessage");

Now only the clients with a subscription for the topic "chatmessage" will get the data

Then you can ofcourse do better pub/sub or rpc with the

PublishTo<T>(Func<T,bool> exp, object o, string topic)

or the

InvokeTo<T>(Func<T,bool> exp, object o, string topic)

Soo... if you want to write a client I suggest you contact XSockets to get all the information about what to send to do a publish/subscribe etc...

Uffe
  • 2,275
  • 1
  • 13
  • 9
  • thank you for the detailed answer. one more question: it is not possible to "listen" to a XSocket with a simple websocket library? as I have understood you, it is impossible, isn't it? – DiKorsch Jun 11 '14 at 12:02
  • Sure. If using RPC from within XSockets just send the mesage and it will arrive at the connected client. If using PUB/SUB you have to make a subscription to a topic before getting any messages. But first of all you would have to connect (handshake) and then tell the server what controller(s) you are communicating over. See the 4.0 alpha docs here http://bit.ly/1xzgXSL – Uffe Jun 11 '14 at 12:46