How do I send a message to every client?
Hi forums,
I'm encountering a lot of frustration here...I'm trying to have one player send a message to the server. The server will then, in turn, send this message to all the clients. I can't get this simple thing to work.
in Fusion my code is as follows
-upon pressing 'p'
+Send text "One" to server on subchannel 8
-On Text Server Message on subchannel 8 (sent)
+End the application
in python:
if message.subchannel == 8:
self.sendMessage("Whatever", 8)
The self in the python code is the server. I've built this off the example code from Mathias's account server. I would assume that the server is sending this message out to everyone logged in, but this results in only the player who sent the message to receive it (and thus end the application).
Oddly enough, I have similar setups in other parts of my code that seem to work fine. Why isn't this working? Did I find a bug?
Thanks in advance for the help...
Re: How do I send a message to every client?
Well, I've never used POINC before but to me it looks like you've just explained the bug to yourself.
In the receive handler for the individual client, you're using "self" to send a message - and you say only that client receives it. Don't you think "self", in this situation, must be the client that triggered the handler?
Re: How do I send a message to every client?
You'd have to do:
Code:
if message.subchannel == 8:
for connection in self.factory.connections:
if connection.account is not None:
connection.sendMessage("Whatever", 8)
"self" actually refers to the protocol (aka connection) instance. "self.factory" will refer to the factory (aka server), so if you need to share anything between connections, do it through the factory.
It would probably be an idea to make a factory function ("sendGlobal", perhaps?) that'll send messages to logged in accounts
Again, you're better off telling us what you're actually trying to achieve ;)
Re: How do I send a message to every client?
Thanks guys. The code Mathias gave me worked. Just a lack of knowledge of the libraries on my part.
And for when I ask for help, I generally try to ask as simple a question as I can. I feel that if I provide the whole problem, in the context of my project, that it will be too convoluted to find a good solution, as well as not provide as much help to others. What I provide is generally a distillation of the problem I'm having, after I have narrowed down what it is after hours of debugging.
Thanks though, everything is working great!