我需要为使用J2EE技术开发并部署在JBoss服务器中的网站添加视频会议功能。 经过多次研究后我找到了WebRTC,但我想知道如何将它与jboss一起使用,因为大多数例子都将它用于node.js 一个简单的代码示例会很有帮助。 谢谢。
修改
我试过webRTC,但我在信号部分遇到了一些问题:
我使用webSocket,这是我的类代码
package com.bean;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ApplicationScoped;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
@ApplicationScoped
@ServerEndpoint("/webrtc")
public class WebRTCSocket {
public static Map < String, Hashtable < String, Object[] >> sessionMap = new Hashtable < String, Hashtable < String, Object[] >> ();
@SuppressWarnings("unchecked")
@OnMessage
public void message(String message, Session session) {
System.out.println("entre a la methode");
try {
JSONObject jsonObject = new JSONObject(message);
try {
Object isOpen = jsonObject.get("open");
if (isOpen != null && (Boolean) isOpen == true) {
String channel = (String) jsonObject.get("channel");
Object value = sessionMap.get(channel);
Hashtable < String, Object[] > sourceDestMap = null;
if (value == null) {
sourceDestMap = new Hashtable < String, Object[] > ();
} else sourceDestMap = (Hashtable < String, Object[] > ) value;
sourceDestMap.put(session.getId(), new Object[] {
session
});
sessionMap.put(channel, sourceDestMap);
}
} catch (JSONException je) {
je.printStackTrace();
}
try {
Object dataObj = jsonObject.get("data");
JSONObject dataMapObj = (JSONObject) dataObj;
//Object thisUserId = dataMapObj.get("userid");
String channel = null;
try {
channel = (String) dataMapObj.get("sessionid");
} catch (JSONException json) {
channel = (String) jsonObject.get("channel");
}
/*
JSONObject dataMapObj = (JSONObject) dataObj;
Object thisUserId = dataMapObj.get("userid");
String channel = (String) dataMapObj.get("sessionid");
Hashtable < string, object > sourceDestMap = sessionMap.get(channel);
if (thisUserId != null && sourceDestMap.get((String) thisUserId) == null) {
sourceDestMap.put((String) thisUserId, new Object[] {
message, session
});
}
for (String userId: sourceDestMap.keySet()) {
if (!userId.equals(thisUserId)) {
Session otherSession = (Session)((Object[]) sourceDestMap.get(userId))[1];
otherSession.getBasicRemote().sendText(message);
}
}
*/
Hashtable < String, Object[] > sourceDestMap = sessionMap.get(channel);
if (sourceDestMap != null)
for (String id: sourceDestMap.keySet()) {
if (!id.equals(session.getId())) {
Session otherSession = (Session)((Object[]) sourceDestMap.get(id))[0];
if (otherSession.isOpen())
otherSession.getBasicRemote().sendText(dataMapObj.toString());
}
}
} catch (JSONException je) {
je.printStackTrace();
}
} catch (JSONException je) {
je.printStackTrace();
}
catch (IOException je) {
je.printStackTrace();
}
System.out.println("Message received:" + message);
}
@OnOpen
public void open(Session session) {
System.out.println("Channel opened");
}
// need to implement @OnClose too
@OnError
public void onError(Throwable error) {
Logger.getLogger(WebRTCSocket.class.getName()).log(Level.SEVERE, null, error);
}
}
这是我的xhtml页面
<html>
<head>
</head>
<body>
<script >
var localVideo;
var remoteVideo;
var peerConnection;
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
function pageReady() {
localVideo = document.getElementById('localVideo');
remoteVideo = document.getElementById('remoteVideo');
serverConnection = new WebSocket('ws://localhost:8080/jasper/webrtc');
console.log('serverConnection' + serverConnection.url);
serverConnection.onmessage = gotMessageFromServer;
var constraints = {
video: true,
audio: true,
};
if(navigator.getUserMedia) {
navigator.getUserMedia(constraints, getUserMediaSuccess, errorHandler);
} else {
alert('Your browser does not support getUserMedia API');
}
}
function getUserMediaSuccess(stream) {
localStream = stream;
localVideo.src = window.URL.createObjectURL(stream);
}
function start(isCaller) {
peerConnection = new RTCPeerConnection(peerConnectionConfig);
peerConnection.onicecandidate = gotIceCandidate;
peerConnection.onaddstream = gotRemoteStream;
peerConnection.addStream(localStream);
if(isCaller) {
peerConnection.createOffer(gotDescription, errorHandler);
}
}
function gotMessageFromServer(event) {
console.log('gotMessageFromServer' + JSON.parse(event.data));
if(!peerConnection) start(false);
var signal = JSON.parse(event.data);
if(signal.sdp) {
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
peerConnection.createAnswer(gotDescription, errorHandler);
}, errorHandler);
} else if(signal.ice) {
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
}
}
function gotIceCandidate(event) {
if(event.candidate != null) {
serverConnection.send(JSON.stringify({'ice': event.candidate}));
}
}
function gotDescription(description) {
console.log('got description');
peerConnection.setLocalDescription(description, function () {
serverConnection.send(JSON.stringify({'sdp': description}));
}, function() {console.log('set description error')});
}
function gotRemoteStream(event) {
console.log('got remote stream');
remoteVideo.src = window.URL.createObjectURL(event.stream);
}
function errorHandler(error) {
console.log(error);
}
</script>
<video id="localVideo" autoplay="autoplay" style="width:40%;"></video>
<video id="remoteVideo" autoplay style="width:40%;"></video>
<br />
<input type="button" id="start" onclick="start(true)" value="Start Video"></input>
<script type="text/javascript">
pageReady();
</script>
</body>
</html>
我使用Jboss-as-7.1.0 final 所以我添加了这个依赖
<dependency>
<groupId>org.jboss.spec.javax.websocket</groupId>
<artifactId>jboss-websocket-api_1.0_spec</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
在控制台中我收到了这个错误:
与'ws:// localhost:8080 / jasper / webrtc'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:404
修改 我发现JBoss as 7.1.0不支持websocket
答案 0 :(得分:0)
java代码
package org.acode;
import java.util.Hashtable;
import java.util.Map;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
@ServerEndpoint("/webrtc")
public class WebRTCSocket {
public static Map<String, Hashtable<String, Object[]>> sessionMap = new Hashtable<>();
@OnMessage
public void message(String message, Session session) {
try {
System.out.println("receive message: " + message);
JSONObject jsonObject = new JSONObject(message);
String channel = null;
JSONObject dataMapObj = null;
Hashtable<String, Object[]> sourceDestMap = null;
try {
boolean isOpen = jsonObject.optBoolean("open");
channel = (String) jsonObject.get("channel");
sourceDestMap = (Hashtable<String, Object[]>) sessionMap.get(channel);
if (sourceDestMap == null) {
sourceDestMap = new Hashtable<String, Object[]>();
}
if (isOpen) {
/* Открытие канала */
sourceDestMap.put(session.getId(), new Object[]{session});
sessionMap.put(channel, sourceDestMap);
} else {
/*Остальные переговоры*/
dataMapObj = jsonObject.optJSONObject("data");
if (dataMapObj != null) {
sourceDestMap = sessionMap.get(channel);
for (String id : sourceDestMap.keySet()) {
if (!id.equals(session.getId())) {
Session otherSession = (Session) ((Object[]) sourceDestMap.get(id))[0];
if (otherSession.isOpen()) {
otherSession.getBasicRemote().sendText(dataMapObj.toString());
}
}
}
}
}
} catch (Exception je) {
je.printStackTrace();
}
} catch (JSONException je) {
je.printStackTrace();
}
}
@OnOpen
public void open(Session session
) {
System.out.println("Channel opened");
}
// need to implement @OnClose too
}
js openSignallingChannel
var SIGNALING_SERVER = "ws://192.168.1.46:8080/webrtc";
connection.openSignalingChannel = function (config) {
config.channel = config.channel || this.channel;
var websocket = new WebSocket(SIGNALING_SERVER);
websocket.channel = config.channel;
websocket.onopen = function () {
websocket.push(JSON.stringify({
open: true,
channel: config.channel
}));
if (config.callback)
config.callback(websocket);
};
websocket.onmessage = function (event) {
config.onmessage(JSON.parse(event.data));
};
websocket.push = websocket.send;
websocket.send = function (data) {
websocket.push(JSON.stringify({
data: data,
channel: config.channel
}));
};
};