简体   繁体   中英

Javascript cannot connect to websocket server endpoint

I have a java web application running on my private JVM on MochaHost. Application is running fine expect the websocket.

I have a websocket endpoint on my JAVA application. I use annotations.

@ServerEndpoint(value = "/websocket/chat/{room}", configurator = ServletAwareConfig.class)

My domain is www.instacollaboration.com. The application is working fine in general except my java script client cannot connect to the websocket server end point.

var Chat = {};
Chat.socket = null;

Chat.connect = (function(host) {
    if ('WebSocket' in window) {
        Chat.socket = new WebSocket(host);
    } else if ('MozWebSocket' in window) {
        Chat.socket = new MozWebSocket(host);
    } else {
        Console.log('Error: WebSocket is not supported by this browser.');
        return;
    }

    Chat.socket.onopen = function() {
        Console.log('Info: WebSocket connection opened. Meeting Room#' + myMeeringRoomNum);
        document.getElementById('chat').onkeydown = function(event) {
            if (event.keyCode == 13) {
                Chat.sendMessage();
            }
        };
    };

    Chat.socket.onclose = function() {
        document.getElementById('chat').onkeydown = null;
        Console.log('Info: WebSocket closed.');
    };

    Chat.socket.onmessage = function(message) {
        // Console.log(message.data);
        processCommands(message.data);
    };
});

Chat.initialize = function() {
    var url = window.location.host + '/websocket/chat/';

    if (window.location.protocol == 'http:') {
        Chat.connect('ws://' + url + myMeeringRoomNum);
    } else {
        Chat.connect('wss://' + url + myMeeringRoomNum);
    }
};

I am seeing this error.

Firefox can't establish a connection to the server at ws://instacollaboration.com/websocket/chat/Y6LA.

Am I missing something? Do mochahost support websockets?

My application and websocket connection run fine on my local tomcat server. This problem is only when running on remote server on MochaHost.

Just chatted with MochaHost support. They don't support websocket on shared servers. I have to buy/rent a private server. :(

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM