Sockjs + Tomcat8 websockets 404错误

时间:2015-03-13 15:02:25

标签: java javascript tomcat websocket socket.io

我开发了一个简单的原生websocket + tomcat 8 websocket示例应用程序正常工作。

但是当我使用 sockjs.js 而不是原生websocket时,我发现了 404 错误。

我在浏览器控制台中收到错误

获取http://localhost:8082/WebsocketsWithFallback/websocket/DataPublisher/info 404(未找到)

这是我的javascript代码,

var ws = null;
var sessionId = null;
var target = "/WebsocketsWithFallback/websocket/DataPublisher";
var connectionLive = false;
var init = false;
var pageCount = 0;
var delay = 0;

$(document).ready(function(){

        if(sessionId == null && init == false){
            sendSessionId( $.trim($('#init').val()) );
        }
});


function sendSessionId(session){
    init = true;
    sessionId = session;
    // console.log("sessionId :"+sessionId);

    if(sessionId != null ){
        connect();
    }
}

function connect() {

    try{

        if(connectionLive == false){
            updateTarget(target);
        }

        if(ws == null){

            try{
                ws = new SockJS(target);
            }
            catch(e){
                alert("WebSocket connection error :"+e.description);
                setTimeout(function(){ connect(); }, 5000);
                return;
            }
        }

        try{

            ws.onopen = function () {
                console.log('Info: WebSocket connection was opened.');
                initMessage();
                connectionLive = true;
            };

        }
        catch(e){
            alert("WebSocket connection open error :"+e.description);
        }


        try{
            ws.onmessage = function (event) {
                // console.log('Received: ' + event.data);
                //Process the data to UI
            };
        }
        catch(e){
            alert("WebSocket message send error :"+e.description);
        }

        try{
            ws.onclose = function (event) {
                console.log('Info: WebSocket connection closed, Code: ' + event.code + (event.reason == "" ? "" : ", Reason: " + event.reason));
                ws = null;
                setTimeout(function(){ connect(); }, 5000);
            };
        }
        catch(e){
            alert("webSocket connection closed Error :"+e.description);
        }
    }catch(e){
        alert(e.description);
    }

}

function disconnect() {
    if (ws != null) {
        ws.close();
        ws = null;
    }
}

function sendData(message) {
    if (ws != null) {
        // console.log('Sent: ' + message);
        ws.send(message);
    } 
}

function updateTarget(val) {
    var target = window.location.host + val;

}


//Send initial message to pass parameters
function initMessage(){
    if(ws != null){
        ws.send(sessionId);
    }
}

我的服务器端将是,

@ServerEndpoint("/websocket/DataPublisher/")
public class Test {


    @OnMessage
    public void pushData(Session session, String msg, boolean last) {
        session.getBasicRemote().sendText(msg, last);

    }

    @OnOpen
    public void openWebSocket(Session session) {
        // My stuff
    }

    @OnClose
    public void closeWebSocket(Session session) {
        // My stuff
    }

    @OnError
    public void errorWebSocket(Throwable exception,Session session) {

    }

}

请提出任何想法或建议。谢谢。

0 个答案:

没有答案