这是我想出的一种方法。我还没有测试代码,所以我不确定它是否有效。另外,我怀疑这是否有效。如果您有更好的解决方案,请在我的代码中查看问题,或者任何建议随时详细说明。这背后的想法是让 WebSocket 服务器端点过滤消息并将它们发送给适当的用户。
首先,我使用预先确定的用户 ID 向客户端发送消息。 WebSocket 服务器端点只知道包含会话 id 的 Session 对象(不能更改;没有设置方法)。为了链接会话 ID 和用户 ID,我需要一个数据库表(比将其保存在内存中更可靠)。
然后,从客户端发送到 WebSocket 服务器端点的第一条消息将包含该用户的 id。这将与会话 ID 一起映射到数据库,并且客户端将被视为“已连接”。当然,这发生在实际连接到 WebSocket 之后。
现在,一旦“连接”,我们就可以向另一个用户发送(信号)消息,只要我们知道他们的用户 ID(我假设我们知道)。 WebSocket 服务器端点根据数据库检查收件人用户 ID 以找到他们的会话 ID。找到后,就可以发送消息了。
最后,在客户端,我们可以对消息进行解密并发送回相应的消息(使用与上述相同的过程)。
因此,如前所述,我们需要一个用于客户端会话和用户 ID 的数据库表。由于我对所有后端代码都使用 Java,我们将通过创建一个实体类来保存属性来使用 ORM(对象关系映射)。像这样的:
@Entity
public class WebSocketUser {
@Id@GeneratedValue
private long id;
private long userId;
private long sessionId;
//constructors, getters and setters
}
现在,我们可以制作 Server Endpoint 类了:
@ServerEndpoint("/SignalingServerEndpoint")
public class SignalingServerEndpoint {
//these class variables will be useful for
//access to the database and such
private EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
private TypedQuery<WebsocketUser> query;
private WebsocketUser wsu;
由于我们不在 EJB 中,我们必须像在应用程序管理环境中一样控制实体管理器。将 onOpen 和 onClose 方法添加到 Websocket:
@OnOpen
public void open(Session session, Endpoint config){
emf = Persistence.createEntityManagerFactory(""); //TODO add persistence unit reference here
em = emf.createEntityManager();
tx = em.getTransaction();
}
@OnClose
public void close(Session session, CloseReason reason){
//if session is closing and information still exists in the database; remove it
if (!wsu.equals(null)){
tx.begin();
em.remove(wsu);
tx.commit();
}
em.close();
emf.close();
}
接下来,在 WebSocket Server Endpoint 的 onMessage 方法中,我们过滤消息。我选择以 JSON 格式发送消息。这使您可以轻松破译信息(我使用了 org.json 库)。 onMessage 方法:
@OnMessage
public void message(Session session, String msg){
try {
JSONObject obj = new JSONObject(msg);
if (!obj.has("toUserId")){
//adding user to the database, so they can access their session ID with their user ID
WebsocketUser wsu = new WebsocketUser(obj.getLong("userId"), session.getId());
tx.begin();
em.persist(wsu);
tx.commit();
}else if (obj.has("toUserId")){
//message to be sent to the user with the specified user ID
//get session ID from database
query = em.createQuery("SELECT u FROM WebsocketUser u WHERE u.userId = " + obj.getLong("toUserId"), WebsocketUser.class);
wsu = (WebsocketUser) query.getSingleResult();
Set<Session> sessions = session.getOpenSessions();
for (Iterator<Session> i = sessions.iterator(); i.hasNext();){
Session s = i.next();
if (s.getId().equals(wsu.getSessionId())){
s.getAsyncRemote().sendText(obj.getString("message"));
break;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
最后,我们只剩下客户端(javascript)了。创建 WebSocket 变量:
var signalingSocket = new WebSocket("/SignalingServerEndpoint"); //TODO need the full and correct path to the WebSocket
现在,对于将发送消息“连接”到 WebSocket 服务器端点的方法:
function connect(){
var msg = {
"userId": userId
};
signalingSocket.send(JSON.stringify(msg));
最后,我们只有客户端的 onMessage 方法(它将解密消息并可能将信息发送到另一个客户端)和所有实际的信令代码(ICE 服务器、约束等)。我不会涉及所有的信号工作,但有一个很好的教程here. 我希望这可以帮助其他面临类似问题的人。正如我所说,我没有测试过代码,所以我不确定它是否会起作用。此外,我非常怀疑这是否有效。但这至少是一个开始。