By Guest on Wednesday, 25 January 2023
Posted in General
Replies 8
Likes 0
Views 1.3K
Votes 0
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);
});
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
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
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi, Sergio

Thank you so much !
·
1 year ago
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post