Anonymous User
  Wednesday, 25 January 2023
  8 Replies
  1.3K Visits
  Subscribe
Hi there,

I need help.
sgcWebSocketServer (Win64 version on Windows10) doesn't send messages.

Connections are accepted (slave open, master open are shown) but messages are never shown so I think sgcWebSocketServer doen't accept sending messages.

Any help ?


const socket = new WebSocket('ws://localhost:8080');

socket.addEventListener('open', function (event) {
console.log("slave open")
socket.send({"message": "boardcast", "params": {"text": "hello from slave"}});
});

socket.addEventListener('message', function (event) {
console.log('Message to slave', event.data);
});



const socket = new WebSocket('ws://localhost:8080');

socket.addEventListener('open', function (event) {
console.log("master open")
socket.send({"message": "boardcast", "params": {"text": "hello from master"}});
});

socket.addEventListener('message', function (event) {
console.log('Message to master ', event.data);
});
1 year ago
·
#1385
Hello,

When the websocket javascript client connects to the server, it sends the following text message:


socket.send({"message": "boardcast", "params": {"text": "hello from master"}});


This message is received by the server, but if you don't call the Broadcast method inside the OnMessage event of the server, the message won't be broadcasted to all clients connected.


procedure TfrmServerChat.WSServerMessage(Connection: TsgcWSConnection; const Text: string);
begin
TsgcWebSocketServer1.Broadcast(Text);
end;


Kind Regards,
Sergio
1 year ago
·
#1386
Hi, Sergio.
Thank you for your reply.

This message is received by the server, but if you don't call the Broadcast method inside the OnMessage event of the server, the message won't be broadcasted to all clients connected.


hmm ? How can I do it with javascript ?
I just downloaded the sgcWebSocketServer binary from here.

I thought I don't have to build sgcWebSocketServer by myself.

Thanks.
1 year ago
·
#1387
Ok, I thought you were using the sgcWebSockets trial for delphi or .net
Yes, with this compiled server you can send a broadcast message from the client, but your message is incorrect, you are using the method "boardcast" instead of "broadcast"
Try with this code


socket.send({"message": "broadcast", "params": {"text": "hello from master"}});


Kind Regards,
Sergio
1 year ago
·
#1388
oh....

I tried what you gave me ("broadcast") but no luck.

So I changed the websocket server to the online test websocket server (wss://demo.piesocket.com/v3/channel_123?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&notify_self) then my code worked well.

I guess connecting to localhost is causing a problem.
1 year ago
·
#1389
Ok, I've dug into the code of the server and the server doesn't broadcast the message if it receives the message "broadcast" FROM A CLIENT. It will broadcast the message, if is the server who calls this method:

{"message": "broadcast", "params": {"text": "hello from master"}}

But this message must be called from the server not from the client. There is no broadcast method from the client side.
I'll implement, it's quite easy to do, I'll post here.

Kind Regards,
Sergio
1 year ago
·
#1390
Hi, Sergio.

I'll implement, it's quite easy to do, I'll post here.

OMG, That's so great !

Thanks a lot.
1 year ago
·
#1391
Hello,

I've updated the WebSocket server application, now you can send a command to broadcast a message FROM the client.
To broadcast a message using a javascript websocket client, send a message that starts with


#sgc_broadcast:


Example, to broadcast the message "hello from master", use the following


socket.send("#sgc_broadcast:hello from master");


Kind Regards,
Sergio
1 year ago
·
#1392
Hi, Sergio

Thank you so much !
  • Page :
  • 1
There are no replies made for this post yet.
Submit Your Response
Upload files or images for this discussion by clicking on the upload button below.
Supported: gif,jpg,png,jpeg,zip,rar,pdf,pas,dfm,dpr,dproj,dpk
· Insert · Remove
  Upload Files (Maximum 10MB)