我正在尝试为使用webrtc的简单Chatroom系统设置java / jetty websocket信令服务器。这只是我想要制作的多人游戏的前奏。我坚持的是当有人加入聊天室时如何通知其他同行,以及如何告诉这位新同伴关于谁已经进入聊天室。同样在客户端上,websocket.onopen事件似乎没有被触发,但是在服务器上,onOpen在客户端连接时被触发。
我使用了一些Muaz的代码,但我找不到兼容的例子。我显然错过了SDP部分。
<script src="https://cdn.webrtc-experiment.com/DataChannel.js"> </script>
<script>
var chatRoom = document.getElementById('chat-room');
var chatOutput = document.getElementById('chat-output');
var chatInput = document.getElementById('chat-input');
chatInput.onkeypress = function(e) {
if (e.keyCode != 13) return;
channel.send(this.value);
chatOutput.innerHTML = 'Me: ' + this.value + '<hr />'
+ chatOutput.innerHTML;
this.value = '';
};
roomName =chatRoom.value;
var channel = new DataChannel('Session Unique Identifier');
channel.onopen = function(userid) {
chatInput.disabled = false;
chatInput.value = 'Hi, ' + userid;
chatInput.focus();
};
channel.onmessage = function(message, userid) {
chatOutput.innerHTML = userid + ': ' + message + '<hr />'
+ chatOutput.innerHTML;
};
channel.onleave = function(userid) {
chatOutput.innerHTML = userid + ' Left.<hr />'
+ chatOutput.innerHTML;
};
var websocket = new WebSocket('ws://localhost:8080/events/');
channel.openSignalingChannel = function (config) {
config.channel = config.channel || this.channel;
websocket.channel = config.channel;
websocket.onopen = function() {
console.log("in onopen");// code is not reached for some reason
// console.log(JSON.stringify({open: true,channel: config.channel}));
websocket.push(JSON.stringify({
open: true,
channel: config.channel,
op : "JoinRoom",
RoomName : roomName
}));
if (config.callback)
config.callback(websocket);
};
websocket.onmessage = function(event) {
config.onmessage(JSON.parse(event.data));
};
websocket.push = websocket.send;
websocket.send = function(data) {
console.log(JSON.stringify({data: data,channel: config.channel}));
websocket.push(JSON.stringify({
data: data,
channel: config.channel
}));
};
};
</script>
对于我的WebSocketServer:
@WebSocket
@ServerEndpoint(value="/events/")
public class SignallingSocket extends WebSocketHandler{
private static Map<String,ArrayList<Session>> rooms = new HashMap<String,ArrayList<Session>>(); //Collections.synchronizedSet(new HashSet<Session>());
Session sess;
String currentRoom="NOTSET";
JSONParser parser=new JSONParser();
@OnMessage
public String onMessage(String message) {
//sess.getContainer().
System.out.println("I just got " + message);
Map json;
try {
json = (Map)parser.parse(message);
String op = json.get("op").toString();
if(op.equals("JoinRoom")){
currentRoom = json.get("RoomName").toString();
if(!rooms.containsKey(currentRoom)){
rooms.put(currentRoom, new ArrayList<Session>());
}
ArrayList<Session> othersInRoom = rooms.get(currentRoom);
for (Session otherPeer : othersInRoom) {
try{
otherPeer.getBasicRemote().sendText("Tell other peers in the room this new person has joined"); //How???
} catch (Exception ex) {
System.out.println("Broadcast error: "+ex.getMessage());
}
}
sess.getBasicRemote().sendText("Tell this new peer about others already in the room");//How???
rooms.get(currentRoom).add(sess);
}
}catch (Exception e) {
System.out.println("Broadcast error: "+e.getMessage());
e.printStackTrace();
}
return "{\"someresponse\":\"What should i return\"}";
}
@OnOpen
public void onOpen (Session peer) { //This works
this.sess = peer;
System.out.println("A client connected");
}
@OnClose
public void onClose(Session peer) {
//peers.remove(peer);
rooms.get(currentRoom).remove(peer);
ArrayList<Session> othersInRoom = rooms.get(currentRoom);
if(othersInRoom.size()==0){
rooms.remove(currentRoom);
}else{
for (Session otherPeer : othersInRoom) {
try{
otherPeer.getBasicRemote().sendText("Tell other peers in the room this person has left");//How???
} catch (Exception ex) {
System.out.println("Broadcast error: "+ex.getMessage());
}
}
}
}
@OnError
public void onError(Throwable t) {
t.printStackTrace();
}
//@Override
public void configure(WebSocketServletFactory factory) {
factory.register(SignallingSocket.class);
}
}