そのためのアーキテクチャを設計する必要があります。
クライアントがサーバーとの接続を確立する (WebSocket を開く) とき、サーバーはどこかに接続を保持する必要があります (使用している Java バックエンドとの特定の接続をどのように識別しても)、依存するデータ構造であなたがやろうとしていること。適切な識別子は、ユーザーが提供する ID です (同じサーバーに接続されている別のピアによってまだ選択されていないニックネームなど)。それ以外の場合は、単純にソケット オブジェクトを一意の識別子として使用し、フロントエンドで他のユーザーを一覧表示するときに、クライアントが特定のピアにメッセージを送信できるように、それらを一意の識別子に関連付けます。
クライアントが別の特定のクライアントとチャットする場合、データ構造にはAHashMap
が適しています。クライアントの一意の ID をソケットにマップし、ハッシュ テーブルの O(1) でエントリを見つけることができるからです。
クライアントから他のすべてのクライアントにメッセージをブロードキャストしたい場合、HashMap
も ( のようなもので) かなりうまく機能しHashMap.values()
ますが、単純な を使用しList
て、着信メッセージを元の送信者を除くすべての接続されたクライアントに送信できます。
もちろん、クライアントとの接続が失われたときにデータ構造からクライアントを削除することも必要です。これは、WebSocket を使用すると簡単です (使用している Java フレームワークは、ソケットが閉じたときにコールバックする必要があります)。
以下は、 Jetty 9 WebSocket (および JDK 7)を使用した (ほぼ完全な) 例です。
package so.example;
import java.io.IOException;
import java.util.HashMap;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
public class MyWebSocket {
private final static HashMap<String, MyWebSocket> sockets = new HashMap<>();
private Session session;
private String myUniqueId;
private String getMyUniqueId() {
// unique ID from this class' hash code
return Integer.toHexString(this.hashCode());
}
@OnWebSocketConnect
public void onConnect(Session session) {
// save session so we can send
this.session = session;
// this unique ID
this.myUniqueId = this.getMyUniqueId();
// map this unique ID to this connection
MyWebSocket.sockets.put(this.myUniqueId, this);
// send its unique ID to the client (JSON)
this.sendClient(String.format("{\"msg\": \"uniqueId\", \"uniqueId\": \"%s\"}",
this.myUniqueId));
// broadcast this new connection (with its unique ID) to all other connected clients
for (MyWebSocket dstSocket : MyWebSocket.sockets.values()) {
if (dstSocket == this) {
// skip me
continue;
}
dstSocket.sendClient(String.format("{\"msg\": \"newClient\", \"newClientId\": \"%s\"}",
this.myUniqueId));
}
}
@OnWebSocketMessage
public void onMsg(String msg) {
/*
* process message here with whatever JSON library or protocol you like
* to get the destination unique ID from the client and the actual message
* to be sent (not shown). also, make sure to escape the message string
* for further JSON inclusion.
*/
String destUniqueId = ...;
String escapedMessage = ...;
// is the destination client connected?
if (!MyWebSocket.sockets.containsKey(destUniqueId)) {
this.sendError(String.format("destination client %s does not exist", destUniqueId));
return;
}
// send message to destination client
this.sendClient(String.format("{\"msg\": \"message\", \"destId\": \"%s\", \"message\": \"%s\"}",
destUniqueId, escapedMessage));
}
@OnWebSocketClose
public void onClose(Session session, int statusCode, String reason) {
if (MyWebSocket.sockets.containsKey(this.myUniqueId)) {
// remove connection
MyWebSocket.sockets.remove(this.myUniqueId);
// broadcast this lost connection to all other connected clients
for (MyWebSocket dstSocket : MyWebSocket.sockets.values()) {
if (dstSocket == this) {
// skip me
continue;
}
dstSocket.sendClient(String.format("{\"msg\": \"lostClient\", \"lostClientId\": \"%s\"}",
this.myUniqueId));
}
}
}
private void sendClient(String str) {
try {
this.session.getRemote().sendString(str);
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendError(String err) {
this.sendClient(String.format("{\"msg\": \"error\", \"error\": \"%s\"}", err));
}
}
コードは自明です。JSON のフォーマットと解析に関して、Jetty には package 内にいくつかの興味深いユーティリティがありますorg.eclipse.jetty.util.ajax
。
また、WebSocket サーバー フレームワークがスレッド セーフでない場合は、データ構造を同期して、データの破損がないことを確認する必要があることに注意してください (こちらMyWebSocket.sockets
)。