【问题标题】:how to send messages using socket.io如何使用 socket.io 发送消息
【发布时间】:2012-04-09 21:29:36
【问题描述】:

我想使用 socket.io 和 node 作为我的“推送通知功能”的层,所以我同时运行 apache 和 node。

我的服务器(节点)上有以下代码

var app = require('http').createServer(handler)
    , io = require('C:/path/to/file/socket.io').listen(app)
    , fs = require('fs');

app.listen(8080);

function handler(req, res) {
    console.log(req);
    fs.readFile('C:/path/to/file/index.html',
        function (err, data) {
            if (err) {
                console.log(err);
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        });
}

io.sockets.on('connection', function (socket) {
    socket.on('my event', function (msg) {
        console.log("DATA!!!");
    });
});

然后页面由 apache 从没有 8080 端口的 localhost 提供服务

在客户端我有以下代码:

var socket = io.connect('http://localhost:8080');

当点击按钮时:

socket.emit('my event', {data:"some data"});

我在节点控制台上什么也看不到...这是为什么呢?跨域问题?

更新: 它在 safari 5.1.5 甚至 IE 9 上都可以正常工作,但在 chrome(18.0.1025.151) 或 firefox (11.0) 上却不行……我错过了什么?

这是节点日志:

   info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized 4944162402088095824
   debug - setting request GET /socket.io/1/websocket/4944162402088095824
   debug - set heartbeat interval for client 4944162402088095824
   debug - client authorized for
   debug - websocket writing 1::
   debug - setting request GET /socket.io/1/xhr-polling/4944162402088095824?t=13
33977095905
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client 4944162402088095824

【问题讨论】:

    标签: javascript apache sockets node.js


    【解决方案1】:

    这应该可以正常工作,只需确保在您的 index.html 中有:

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    

    此外,由于您通过 Apache 提供页面,因此您确实不需要节点文件中的处理程序和 http 服务器。 这应该可以正常工作:

    var io = require('socket.io').listen(8080);
    io.sockets.on('connection', function (socket) {
        socket.on('my event', function (msg) {
            console.log("DATA!!!");
        });
    });
    

    对于 index.html :

    <!DOCTYPE html>
    <html lang="en">
    
        <head>
            <title>Hello World!</title>
            <meta charset="utf-8">
    
            <script src="http://localhost:8080/socket.io/socket.io.js"></script>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    var socket = io.connect('http://localhost:8080');
                    $("#button").click(function() {
                        socket.emit('my event' ,"Hello World!");
                    })
                })
            </script>
        </head>
    
        <body>
            <button type="button" id='button'>Send Message</button> 
        </body>
    
    </html>
    

    编辑:这适用于 Firefox 和 Chrome。

    【讨论】:

    • 感谢您的回复!显然我的问题是本地的,您的示例在 safari 和 IE 上运行良好,但在我的 chrome 或 firefox 上运行良好......我应该在哪里看? P.S:我在搞乱 WebRTC,因此在我的 chrome 上启用了此功能(“启用 MediaStream。Mac、Windows、Linux、Chrome 操作系统为 WebRTC 功能启用 MediaStream、GetUserMedia 和 PeerConnection API。更多信息请访问 webrtc.org。” ),尝试禁用它但没有成功......还有其他想法吗?
    • 尝试重新安装 chrome ...不行 :( 更多信息:我在 Windows 7 上运行节点,我使用 socket.io 的完整路径,就像这样 "C:\Users\shlomis \node_modules\socket.io"(想不通)...这可能是相关的吗?
    • 这很奇怪...尝试打开 Chrome 开发工具或 Firefox 上的 Firebug,然后打开“网络”选项卡,它可以为您提供有关幕后发生的更多信息
    • 似乎 FF 和 chrome 在几秒钟后回退到日志轮询......我不知道为什么
    • 更新:我添加了 socket.on('connect', function () { console.log("connected"); });它永远不会在 chrome 上触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2015-12-17
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多