本地部署时的代码连接到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/"
这里可能出现的问题是什么?
答案 0 :(得分:3)
您需要将websocket连接到应用程序URL上的端口8000,而不是端口80。
ws://map-tweetstream.rhcloud.com:8000/tweetstream/tweets