我在openshift中遇到socket.io连接问题。
xhr-polling:http://demowebchat-pydevelopment.rhcloud.com:8000/socket.io/?EIO=3&transport=polling&t=LWuQ0UY&sid=qMdDVygVfAYMbfRbAAAO响应状态400错误请求
我之前搜索并尝试了一些方法,但它无法正常工作
以下是我在客户端尝试创建套接字的一些方法
//Solution 1
var socket = io.connect("http://demowebchat-pydevelopment.rhcloud.com:8080/");
//Solution 2
var socket = new io('http://demowebchat-pydevelopment.rhcloud.com:8000/');
//Solution 3
var socket = new io('ws://demowebchat-pydevelopment.rhcloud.com:8000/');
//Solution 4
var socket = io.connect('ws://demowebchat-pydevelopment.rhcloud.com:8000/');
这是package.json
中的配置"dependencies": {
"socket.io": "latest",
"express": "latest",
"optimist": "latest"
}
以下是我在服务器端的代码
//Declare constants...
//End declare constants
var express = require('express');
var app = express();
var server = require('http').createServer(app);
server.listen(PORT, IPADDRESS,function(){
console.log("Server listen in " + IPADDRESS + ":" + PORT);
});
var io = require('socket.io')(server,{origins:'demowebchat-pydevelopment.rhcloud.com:* http://demowebchat-pydevelopment.rhcloud.com:* http://www.demowebchat-pydevelopment.rhcloud.com:*'});
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
更新:这是我的PORT和IPADDRESS声明
var PORT = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var IPADDRESS = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP || 'localhost';
我的问题在哪里?