0

I created a Websocket server based on websocket-sharp, with two behaviors, ex:

  • ws://host/behavior1
  • ws://host/behavior2

From JavaScript, when I open a connection to the WebSocket Server, I need to specify the URL of the behavior that will accept my message. How can I call behavior2 when I connect on the url of behavior1 without having to create a new websocket connection? Is this possible?

At this point I am considering creating one behavior that will parse my message and redirect to the appropriate message handler.

Is there any reason to use multiple behaviors beside logical division? Any reason to use this logical division even if it forces to create a new connection?

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
SlashJ
  • 687
  • 1
  • 11
  • 31

1 Answers1

0

How can I call behavior2 when I connect on the url of behavior1 without having to create a new websocket connection?

When you have two URLs then you will have to make two different connections to each of the URLs. You cannot reuse the same connection to connect to different URLs.

At this point I am considering creating one behavior that will parse my message and redirect to the appropriate message handler.

This would be the best approach to go for. You can send say json data in WebSocket frames then parse this data to identify the appropriate behavior and process accordingly. For example you can have a JSON object as follows in each of your frames:

{
   "behavior":1,
   "content": "your content to be processed"
}

Is there any reason to use multiple behaviors beside logical division? Any reason to use this logical division even if it forces to create a new connection?

You need to use different URLs only for connection specific data that are different for each client. For example an id or name. If same client need different behaviors at different times you need can define your own convention for data in WebSocket frames as mentioned above.

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55