1

I'm trying to send a message from a client to another client using RPyC.

For the moment I use the following code, I launch the server first then I connect and "register" clients to the server ( I could've done that to the on_connect() method too ... but doesn't change anything as far as I see).

Any advise or help much appreciated, thanks !

To register the clients I do this:

On one shell (client1 => light ):

import h_client
c = h_client._connectToServer()
c.root.registerClient("light")

On Another shell (client2 => dark ):

import h_client
c = h_client._connectToServer()
c.root.registerClient("dark")
c.root.sendDataToClient("light", "msg", "me", "My great message")

The script freezes until I go back to the "light" shell and write something ( like c.root ... ), and then I can see the result on my "light" shell, if I do nothing in the shell it seems that the result never shows up..

Server (h_server.py):

import rpyc
from rpyc.utils.server import ThreadedServer

class HChat_Server(rpyc.Service):

    CLIENTS = {}

    def exposed_registerClient(self, clientID):

        self.CLIENTS[clientID] = self._conn.root.exposed_catchData

    def exposed_getAllClients(self):

        return self.CLIENTS

    def exposed_sendDataToClient(self, clientID, dataType, sender, data):

        self.CLIENTS[clientID](dataType, sender, data)


if __name__ == "__main__":

    t = ThreadedServer(HChat_Server, port = 5000, protocol_config={"allow_public_attrs" : True})
    t.start()

Client (h_client.py):

class HChat_ClientService(rpyc.Service):

    def exposed_catchData(self, dataType, sender, data):

        sys.stdout.write("=> {0}: {1} ({2})".format(sender, data, dataType))

def _connectToServer():

    server_conn = rpyc.connect("localhost", 5000, service=HChat_ClientService)

    return server_conn
GuillaumeJ
  • 66
  • 6

0 Answers0