为了使用WebRTC API
连接对等通信,您需要实现信令。信令需要双向通信通道才能工作。WebSockets
是双向通信通道。我想使用WebSockets
来实现信令,但是,我不知道如何处理这个问题。我在网上找到的所有东西都只是讨论了你可以使用的不同的双向通道,而不是提供一个如何使用它们的例子。
我在客户端使用JavaScript
,服务器端使用Java
。那么,我将如何使用WebSockets
实现信令呢?
发布于 2014-08-04 15:02:11
我想出了一个办法。我还没有测试代码,所以,我不确定它是否有效。而且,我怀疑这是否有效。如果您有更好的解决方案,请在我的代码中看到一个问题,或者任何建议都可以详细说明。这背后的想法是让WebSocket服务器端点过滤器通过消息并将它们发送给适当的用户。
因此,如前所述,我们将需要一个用于客户端会话和用户id的数据库表,因为我在所有后端代码中使用Java,我们将通过创建一个实体类来保存这些属性,从而使用ORM (对象关系映射)。就像这样:
@Entity
public class WebSocketUser {
@Id@GeneratedValue
private long id;
private long userId;
private long sessionId;
//constructors, getters and setters
}
现在,我们可以使类:
@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();
}
接下来,在onMessage服务器端点中的WebSocket方法中,我们对消息进行过滤。我选择以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服务器、约束等)。我不会进入所有的信令工作,但有一个很好的教程这里。,我希望这能帮助其他面临类似问题的人。正如我所说的,我还没有测试代码,所以我不确定它是否能工作。此外,我非常怀疑这是否会有效率。但这至少是一个开始。
https://stackoverflow.com/questions/25019945
复制相似问题