1

Java websocket api を使用して、2 つの html ページ間で簡単なビデオ転送を実装しようとしています。Webcam サーバーは Webcam をキャプチャし、接続されているすべてのクライアントにブロードキャストするサーバー エンドポイントに送信します。ウェブカメラ サーバー コード

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

これは、プロジェクトをダウンロードするためのリンクです

4

1 に答える 1

1

ここであなたのプロジェクトにいくつかの問題があります。まず、Struts と WebSocket の間で URL マッピングが重複しています。WebSocket URL は/endpointwcvです。Struts マッピングは/*. 最終的にアプリをデプロイしたとき、Struts から action のマッピングがないというエラーが表示されました/endpointwcv

Struts フィルター サーブレットのマッピングを に変更した場合/struts/*(および、が非推奨であるというエラー メッセージを回避するためfilter-classに をに変更する必要がありました)、 に移動して WebSocket エンドポイントにアクセスできます。GlassFish4 の server.log に、次の出力が表示されました。org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterFilterDispatcherhttp://localhost:8080/WebApplication5/Conference.htmlNewWSEndpoint

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

に行くとhttp://localhost:8080/WebApplication5/ConferenceClient.html、server.log に次のように表示されます。

INFO: aaaa

私は Struts の専門家ではありませんが、エンドポイントを修正してから、WebSocket エンドポイント URL に干渉しないように Struts フロントエンドを実装する方法を理解する必要があるようです。

于 2013-07-11T18:01:30.347 に答える