Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

java - Sockjs + Tomcat8 websockets 404 error

I have developed a plain native websocket + tomcat 8 websocket sample appliation works fine.

But when I use the sockjs.js , instead of native websocket , I genting 404 error .

I am getting error in my browser console as

GET http://localhost:8082/WebsocketsWithFallback/websocket/DataPublisher/info 404 (Not Found)

Here is my javascript code,

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

My server side will be,

@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) {

    }

}

Please give any ideas or suggesstions.Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...