codeigniter - 无法使用 MAMP + Socket.io (node.js) 连接并出现 "unhandled socket.io url"错误

标签 codeigniter node.js mamp socket.io

我正在 MAMP 服务器上使用 CodeIgniter 开发一个应用程序(对于开发,实时它将在 LAMP 上)。我正在尝试为聊天服务器添加 socket.io 的实时功能,但遇到了一些问题。我已经独立启动并运行了 socket.io 和 MAMP,但我无法让我的客户端与我的服务器通信。

服务器.js:

// Get the Socket.io module
var io = require('socket.io');

console.log ( 'Chat Server started' );

// Create a Socket.IO instance, listen on 8084
var socket = io.listen(8084);

// Add a connect listener
socket.on('connection', function(client){ 

  console.log ( "Server Connected" );

});

我的客户端脚本(socket.io.js 加载正常,每当我加载页面时,我的控制台都会显示“调试:提供静态内容”):

<script src="http://localhost:8084/socket.io/socket.io.js"></script>
<script type="text/javascript"> 
    // Create SocketIO instance, connect
    var socket = new io.Socket('localhost',{
      port: 8084
    });
    socket.connect(); 

    socket.on ( 'connect', function () { console.log ( 'Client connected' ); } );
</script>

启动node.js 文件后,我在控制台中看到以下内容:

Chat Server started
info  - socket.io started

加载客户端后(将我的浏览器定向到 http://localhost:8888 - MAMP 的默认端口),我没有收到任何控制台消息,而是收到稳定的消息流(大约每秒一次):

info  - unhandled socket.io url

看起来根本没有连接。另外,浏览器上的 JS 错误控制台中没有错误。有什么想法吗?

最佳答案

运行socket.io版本0.8.5,我终于使用以下代码完成了这项工作:

服务器.js

var sys = require('sys'),
    express = require('express'),
    app         = express.createServer('localhost'),
    io          = require('socket.io'); 

app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {
    res.send('Hello World');
});

app.listen(3000);

var server = io.listen(app); 

server.sockets.on('connection', function (client){ 
    // new client is here!
    client.send ( 'Now connected!' );

    client.on('message', function () {

    }) ;

    client.on('disconnect', function () {

    });

});

客户端脚本:

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="/assets/js/jquery-1.6.4.min.js"></script>
<script type="text/javascript"> 

    $(document).ready(function(){

        var socket  = io.connect('http://localhost:3000'),
        text = $('#text');

        socket.on('connect', function () {
            text.html('connected');
            socket.on('message', function (msg) {
                text.html(msg);
            });
        });

        socket.on('disconnect', function () {
            text.html('disconnected');
        });

    });

</script> 

一切正常运行后,进入我的页面后,我会看到“现已连接!”几乎立即。我还通过 MAMP 上的 CodeIgniter 提供我的页面 - 一切正常!

关于codeigniter - 无法使用 MAMP + Socket.io (node.js) 连接并出现 "unhandled socket.io url"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7781616/

相关文章:

mysql - 无法连接数据库 - MAMP

php - 为什么 MAMP 不显示错误?

php - CodeIgniter 插入两行而不是一行

node.js - 在Electron BrowserWindow中进行全文本搜索

node.js - Gulp-wrap 替代方案

node.js - 向用户请求参数或触发履行事件

php - 使用Mamp和MySQL(PHP)“无法连接”

php - 网站模板/设计建议-后端

jquery - 使 jquery 表可点击

javascript - 通过 AJax 传递变量