gpt4 book ai didi

java - 异常启动过滤器WebSocket过滤器netbeans 7.3 glassfish 4

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:50 25 4
gpt4 key购买 nike

我正在尝试使用 java websocket api 实现两个 html 页面之间的简单视频传输。网络摄像头服务器捕获网络摄像头并将其发送到服务器端点,该端点向每个连接的客户端广播。网络摄像头服务器代码

<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);

服务器端点的代码 /* 在这里进口*/

@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");
}
}

并且有一个接收客户端访问网络摄像头

<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>

使用 java ee 7 api 在 GlassFish 4.0 上运行项目时,没有任何反应。请帮助我。控制台上有一个警告

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)
...

This is the link to download the project

最佳答案

我在这里发现您的项目存在一些问题。首先,Struts 和 WebSocket 之间存在 URL 映射重叠。您的 WebSocket URL 是 /endpointwcv。您的 Struts 映射是 /*。当我最终部署您的应用程序时,我从 Struts 收到错误,说明操作 /endpointwcv 没有映射。

如果您将 Struts 过滤器 servlet 的映射更改为 /struts/*(并且我必须将 filter-class 更改为 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 以避免有关 FilterDispatcher 已弃用的错误消息),您可以通过访问 http://localhost:808 来访问 WebSocket 端点0/WebApplication5/Conference.html。在 GlassFish4 server.log 中,我看到来自 NewWSEndpoint 的输出:

INFO: aaaa
INFO: error
INFO: error
INFO: error
...
INFO: Closed

转到http://localhost:8080/WebApplication5/ConferenceClient.html,我在server.log中看到:

INFO: aaaa

我不是 Struts 专家,但看起来您需要修复端点,然后弄清楚如何实现 Struts 前端,以便它不会干扰您的 WebSocket 端点 URL。

关于java - 异常启动过滤器WebSocket过滤器netbeans 7.3 glassfish 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com