JavaScript:部署到Openshift后的Websocket Error

时间:2014-07-24 15:47:03

标签: javascript websocket openshift

本地部署时的代码连接到Websocket端点为

 var connection = new WebSocket('ws://127.0.0.1:8080/tweetstream/tweets');  

我将此部署到Openshift并将URL更改为http://map-tweetstream.rhcloud.com/tweetstream/

现在我开始看到WebSocket Error [object Event]错误,所以在Developer Tools(Chrome) -> Console中,我尝试连接到此websocket

var connection = new WebSocket('ws://'+ document.location.host + document.location.pathname + 'tweets');
  connection.onopen = function() {
    connection.send('brazil');
  };
  connection.onerror = function(error) {
    console.log('WebSocket Error ' + error);
  };
  connection.onmessage = function(e) {
    var parse = JSON.parse(e.data);
    var coordinates = parse["geo"]["coordinates"];
    console.log("coordinates:" + JSON.stringify(coordinates, undefined, 2));
  };  

我得到的是

WebSocket connection to 'ws://map-tweetstream.rhcloud.com/tweetstream/tweets' failed: Error during WebSocket handshake: Unexpected response code: 404
WebSocket Error [object Event]   

我看到的地方

document.location.host
"map-tweetstream.rhcloud.com"
document.location.pathname
"/tweetstream/"  

这里可能出现的问题是什么?

1 个答案:

答案 0 :(得分:3)

您需要将websocket连接到应用程序URL上的端口8000,而不是端口80。

ws://map-tweetstream.rhcloud.com:8000/tweetstream/tweets