【问题标题】:CodeIgniter with Socket.io error:net::ERR_CONNECTION_REFUSEDCodeIgniter 与 Socket.io 错误:net::ERR_CONNECTION_REFUSED
【发布时间】:2015-06-22 06:43:28
【问题描述】:

我想使用 Socket.io 在我的应用程序中添加实时通知,但在将它集成到我的应用程序时遇到了问题。当我运行应用程序并查看浏览器日志(控制台)时,

我的连接被拒绝了

(获取 http://localhost:1500/socket.io/?EIO=3&transport=polling&t=1434954742052-24 网络::ERR_CONNECTION_REFUSED)

我的应用程序位于 localhost:130。我已经阅读了 CORS 并在我的控制器中应用了权限。

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, 
       Content - Type, Accept, Access - Control - Request - Method ");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    die();
}

parent::__construct();

这是我的 server.js

var io = require('socket.io').listen(1500);
io.sockets.on('connection', function (socket) {
// Let everyone know it's working
socket.emit('startup', { message: 'I Am Working!!' });
});

这是我的 client.js

$(function() {
    window.MY_Socket = {
        // Instantiate the Socket.IO client and connect to the server
        socket: io.connect('http://localhost:1500'),

        // This just indicates that a Socket.IO connection has begun.
        startupMessage: function(data) {
            console.log(data.message);
        },
    }
});

我仍然收到error:net::ERR_CONNECTION_REFUSED。我一直在寻找工作,但我仍然可以弄清楚我们如何解决这个问题。期待您的帮助

【问题讨论】:

  • 节点服务器应该发送标头,而不是 PHP。检查这个stackoverflow.com/questions/24058157/…
  • 谢谢@DamirMiladinov,它现在可以工作了。你能把你的帖子作为这个帖子的答案吗?我可以相信你。谢谢

标签: codeigniter socket.io


【解决方案1】:

感谢@DamirMiladinov 帮助我,下面是代码。

var io = require('socket.io').listen(1500); //for the socket
io.set('origins', "http://localhost:130"); //the port of the application
io.sockets.on('connection', function (socket) {
// Let everyone know it's working
socket.emit('startup', { message: 'I Am Working!!' });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2016-04-15
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多