我的代码在本地部署时连接到 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/"
这里可能的问题是什么?
最佳答案
您需要将 websocket 连接到应用程序 url 上的端口 8000,而不是端口 80。
ws://map-tweetstream.rhcloud.com:8000/tweetstream/tweets
关于JavaScript:部署到 Openshift 后出现 Websocket 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24938343/