简体   繁体   English

如何使用 socket.io 发送消息

[英]how to send messages using socket.io

I want to use socket.io and node as a layer for my "push notification feature", so I'm running both apache and node.我想使用 socket.io 和节点作为我的“推送通知功能”的层,所以我同时运行 apache 和节点。

I have the following code on my server (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!!!");
    });
});

the page is then served by apache from localhost without 8080 port然后该页面由 apache 从没有 8080 端口的本地主机提供服务

and on the client I have the following code:在客户端我有以下代码:

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

and when a button is clicked:单击按钮时:

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

I see nothing on the node console... why is that?我在节点控制台上什么也看不到……这是为什么? cross domain issue?跨域问题?

Update: it works just fine on safari 5.1.5 and even IE 9, but not on chrome(18.0.1025.151) or firefox (11.0)... what am I missing?更新:它在 safari 5.1.5 甚至 IE 9 上工作得很好,但在 chrome(18.0.1025.151) 或 firefox (11.0) 上却不行……我错过了什么?

here is the node log:这是节点日志:

   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

That should work fine, just make sure that in your index.html you have:那应该可以正常工作,只需确保在您的 index.html 中您有:

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

also, since you're serving your page via Apache, you really don't need the handler and the http server in you node file.此外,由于您通过 Apache 为您的页面提供服务,因此您实际上不需要节点文件中的处理程序和 http 服务器。 this should work just fine:这应该工作得很好:

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

and for the index.html:对于 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>

Edit: This works in both Firefox and Chrome.编辑:这适用于 Firefox 和 Chrome。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM