xsockets.net - XSocket.net. how to send a message to a client from an object which is not a controller -


i have class starts server:

public class socketserver {     private static ixsocketservercontainer server = null;      public socketserver()     {         server = xsockets.plugin.framework.composable                       .getexport<ixsocketservercontainer>();     }      public bool startservers()     {         try         {             server.startservers();             return true;         } catch         {             return false;         }     } 

this class has method:

public void sendeventmessagetoallclients(string message) {     xsockets.core.xsocket.helpers.xsockethelper         .sendtoall<mycontroller>(new mycontroller(), message, "events"); } 

where mycontroller own controller, implemented , server can find , method work.

now expand functionality new method allows me send event specific client:

    public void sendeventmessagetoclient(string clientid, string message)     {         xsockets.core.xsocket.helpers.xsockethelper              .sendto<mycontroller>(new mycontroller(),                                     p => p.clientid == clientid, message, "events");     } 

is right approach or doing wrong?

thanks!

i not recomend approach, have not tested if actaully works.

you create new controller every time able access extension method. guessing since have on class starting server use publisher?

if correct way install xsockets.client package , use client pool publish messages: client pool documentation

example client pool

the nice thing client pool not need create instance every time. pool reuse connection controller. using clientpool (or real client connection) ensure message pass through pipeline , interceptors if have any. using controller instance directly never reach pipline, interceptors etc.

//get pool client clientpool poolclient =         xsockets.client.clientpool.getinstance("ws://127.0.0.1:4502/mycontroller", "*"); 

methods sending message controller.

public void sendeventmessagetoclient(guid clientid, string message) {             poolclient.send(new {clientid, message}, "sendeventmessagetoclient"); }    public void sendeventmessagetoallclients(string message) {     poolclient.send(message, "sendeventmessagetoallclients");     } 

the controller

public void sendeventmessagetoclient(guid clientid, string message) {     this.sendto(p => p.clientid == clientid, message, "sendeventmessagetoclient"); }    public void sendeventmessagetoallclients(string message) {     this.sendtoall(message, "sendeventmessagetoallclients"); } 

example instance of controller

if decide use way have done should @ least create on 1 instance of controller use in server class.

important: using controller instance directly never reach pipline, interceptors etc.

//be aware of fact controller never have connection. //it can send others, can never receive messages! mycontroller c = new mycontroller();  //you should have guid here instead of string //also note client have subscribe "events" message public void sendeventmessagetoclient(guid clientid, string message) {     this.sendto(p => p.clientid == clientid, message, "sendeventmessagetoclient"); }    public void sendeventmessagetoallclients(string message) {     this.sendtoall(message, "sendeventmessagetoallclients"); } 

since not know trying accomplish im not sure best way, 1 of merhods above should work.

edit: also, in real application dont have access mycontroller class since in separate assembly not being referenced @ compile time. approach not possible , way go client or clientpool

/uffe


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -