TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.48k stars 2.58k forks source link

How do I send a message to a single client? #1

Closed ghost closed 14 years ago

ghost commented 14 years ago

I'm looking at the ChatServer.java example

I see how you can send a message to all web clients using the sendToAll(String) function, but how does one send to just a single web client?

It's not coming up in my Eclipse autocomplete.

Many thanks in advance,

TooTallNate commented 14 years ago

You need to call send(String) on an individual WebSocket instance to send an individual message. These are retrieved as the first parameter from any of the onOpen, onClose or onMessage callbacks, or via the connections property on the WebSocketServer instance.

Perhaps it would be helpful to add a convenience function to the WebSocketServer class: send(WebSocket, String)?

I urge you to look at the precompiled JavaDoc files inside the doc folder inside the repo for any more API questions.

ghost commented 14 years ago

Hey,

Thanks for your reply. Yes, I get a WebSocket object every time onClientOpen(conn) gets called. I then copy this object and can send to an individual browser. It's all working now. Sorry for the schoolboy error. I'm just back to Java after many years of programming in C.

Good project. So much cleaner and more straightforward than jWebSocket which has a crazy website. You provide one jar for the WebSocket server and one jar for the javadocs. Perfect.