Question: 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.

```cs
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");

    }

}

```

Answer: 
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");

    }

}

```
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.