Reputation: 31
I've been trying to solve this simple problem, but I cannot make it work.
I am using WebSocketSharp.Server
. The code can be seen below.
In my NetworkClass
I have someData
I would like to send to the client when a message is received. The problem is the OnMessage
event gets fired in a different class, and I don't know how the access the instance of this class.
Broadcasting to all clients form the NetworkClass
works fine and receiving messages form the client works fine as well.
public class IndexRoute : WebSocketBehavior {
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//TODO: send someData string to client
}
}
public class NetworkClass {
String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
server.AddWebSocketService<IndexRoute>("/");
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
Upvotes: 0
Views: 5042
Reputation: 31
I combined the answers of Jeroen van Langen and Jesper. The key was to pass the instance of the NetworkClass
to the IndexRoute
class and access the variables from there.
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(this));
works but is marked as obsolete.
public class IndexRoute : WebSocketBehavior {
private NetworkClass _instanceOfNetworkClass;
public IndexRoute(NetworkClass instanceOfNetworkClass)
{
_instanceOfNetworkClass = instanceOfNetworkClass;
}
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//Broadcast someData string to clients
Sessions.Broadcast(_instanceOfNetworkClass.someData);
}
}
public class NetworkClass {
public String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(this));
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
Upvotes: 3
Reputation: 7615
When you inherit WebSocketBehavior
, you get methods and properties that allow you to find out information about the connection you are currently talking to. In this case, just use one of the Send
methods to send to the connection that sent the message:
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
Send(/* pick one of the overloads to suit your needs */);
}
You can also use the property Context
to get a WebSocketContext
with information about the connection, or ID
to find the session (connection) ID, which you could pass to another object. If the question is how you find your instance of NetworkClass, you are already constructing the IndexRoute yourself, just pass this
in as an additional parameter.
Upvotes: 0
Reputation: 22083
I don't have any experience with WebSocketSharp, but the docs says you can give an alternate WebSocketBehavior construction function to the AddWebSocketService<>
Let me know if it works: (else i'll remove it)
public class IndexRoute : WebSocketBehavior {
private string _someData;
// define the constructor which accepts the someData
// save it in a field.
public IndexRoute(string someData)
{
_someData = someData;
}
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//TODO: send someData string to client
// do something with _someData
}
}
public class NetworkClass {
String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
// pass the construction function.
// (construct the IndexRoute and pass the someData to it.)
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(someData));
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
I didn't tested it, but it might give you a step in the right direction to solve it.
Upvotes: 0