i am trying to implement simple video transfer between two html pages using java websocket api. The webcam server captures the webcam and sends to server endpoint which broadcasts to every connected clients. The webcam server code
<video autoplay id="vid" style="display: none;"></video>
<canvas id="canvas" width="640" height="480" style="border: 1px solid #d3d3d3;"></canvas>
<div id="data1"></div>
<script>
var video = document.querySelector("#vid");
var canvas = document.querySelector('#canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;
var ws = new WebSocket("ws://127.0.0.1:8080/WebApplication5/endpointwcv");
ws.onopen = function () {
console.log("Openened connection to websocket");
};
ws.onerror = function (evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
};
var onCameraFail = function (e) {
console.log('Camera did not work.', e);
};
timer = setInterval(function () {
ctx.drawImage(video, 0, 0, 640, 480);
var data = canvas.toDataURL('image/jpeg', 1.0);
ws.send(data);
}, 255);
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia({ video: true }, function (stream) {
video.src = window.URL.createObjectURL(stream);
}, onCameraFail);
The code for server endpoint /* Imports here */
@ServerEndpoint("/endpointwcv")
public class NewWSEndpoint {
private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());
@OnMessage
public String onMessage( Session session,byte[] data) {
try {
System.out.println(data);
for(Session s:peers){
s.getBasicRemote().sendObject(data);
}
} catch (IOException | EncodeException e) {
System.out.println("Error in facedetection, ignoring message:" + e.getMessage());
}
return null;
}
public String onMessage( Session session,String data) {
try {
System.out.println(data);
for(Session s:peers){
s.getBasicRemote().sendText(data);
}
} catch (IOException e) {
System.out.println("Error in facedetection, ignoring message:" + e.getMessage());
}
return null;
}
@OnOpen
public void onOpen(Session session) throws IOException {
peers.add(session);
session.getBasicRemote().sendText("hiiiiiii");
}
@OnClose
public void onClose() {
System.out.println("Closed");
}
@OnError
public void onError(Session s, Throwable t) {
System.out.println("error");
}
}
And there is a recieving client who accesses the webcam
<div id="d1"></div>
<canvas id="target" width="640" height="480" style="border: 1px solid #d3d3d3;"></canvas>
<script>
var ws = new WebSocket("ws://127.0.0.1:8080/WebApplication5/endpointwcv");
var myURL = window.URL || window.webkitURL;
ws.onopen = function () {
console.log("Openened connection to websocket");
};
ws.onmessage=function (msg) {
var target = document.getElementById("target");
url=myURL.createObjectURL(msg);
target.src = url;
};
</script>
On Running the project on GlassFish 4.0 with java ee 7 api, nothing happens. Plz help me out.On the console there's a warning
WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
.....
Exception starting filter WebSocket filter
java.lang.InstantiationException
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:135)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:5297)
...
I see a couple of problems with your project here. First of all, you have a URL mapping overlap between Struts and WebSocket. Your WebSocket URL is /endpointwcv
. Your Struts mapping is /*
. When I finally got your app to deploy, I was getting errors from Struts about how there's no mapping for the action /endpointwcv
.
If you change the mapping of the Struts filter servlet to /struts/*
(and I had to change the filter-class
to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
to avoid error messages about how FilterDispatcher
is deprecated), you can access the WebSocket endpoint by going to http://localhost:8080/WebApplication5/Conference.html
. In the GlassFish4 server.log, I was seeing output from NewWSEndpoint
:
INFO: aaaa
INFO: error
INFO: error
INFO: error
...
INFO: Closed
Going to http://localhost:8080/WebApplication5/ConferenceClient.html
, I see in server.log:
INFO: aaaa
I'm not a Struts expert, but it looks like you need to fix your endpoint and then figure out how to implement the Struts front-end so it doesn't interfere with your WebSocket endpoint URL.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.