Javascript 全局变量和 Socket.io

发布于 2024-12-01 10:53:57 字数 1713 浏览 0 评论 0原文

Javascript 中是否有类似于 PHP 全局变量的“全局变量”函数可以应用于本示例?

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
querystring = require('querystring');

app.listen(8000);

var content = '';

function handler(req, res) {
    io.sockets.on('connection', function (socket) {
        if(req.method == 'POST') {
            var fullBody = '';

            req.on('data', function(chunk) {
                fullBody += chunk.toString();
            });

            req.on('end', function() {
                var decodedBody = querystring.parse(fullBody);
                console.log(decodedBody);

                socket.emit('user-aaa5c8bbffe4db9', fullBody);
            });
        }
    });

    fs.readFile(__dirname + '/index.html',
    function(err, data) {
        if(err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

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

我编写了一个 PHP 应用程序,但我想通过 socket.io 收到通知,但我还不想为 Node.js 重写该应用程序,所以我设置了一个 Node 服务器,PHP 将向该服务器发送 POST,然后服务器将通过 Socket.io 发送通知。我已经使用基本脚本测试了服务器,它们确实有效。我还尝试在 IF 语句内移动“io.sockets.on('connection', function (socket) {”:

req.on('end', function() {
    var decodedBody = querystring.parse(fullBody);
    console.log(decodedBody);

    io.sockets.on('connection', function (socket) {
        socket.emit('user-aaa5c8bbffe4db9', fullBody);
    });
});

但这并没有产生“即时”结果,通知会在页面后传入客户端刷新

客户端很简单:

socket.on('user-<?php echo $_SESSION['user_display_id']; ?>', function (data) {
    alert(data);
    $('#events').html(data);
});

任何帮助将不胜感激。

Is there any "globals" function in Javascript which is similar to PHP globals that would apply in this example?

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
querystring = require('querystring');

app.listen(8000);

var content = '';

function handler(req, res) {
    io.sockets.on('connection', function (socket) {
        if(req.method == 'POST') {
            var fullBody = '';

            req.on('data', function(chunk) {
                fullBody += chunk.toString();
            });

            req.on('end', function() {
                var decodedBody = querystring.parse(fullBody);
                console.log(decodedBody);

                socket.emit('user-aaa5c8bbffe4db9', fullBody);
            });
        }
    });

    fs.readFile(__dirname + '/index.html',
    function(err, data) {
        if(err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

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

I wrote a PHP app but I want to have notifications through socket.io but I don't want to rewrite the app for Node.js (yet) so I setup a Node server which the PHP will send a POST to and then the server will send the notification via Socket.io. I've tested the server with basic scripts and they do work. I've also tried moving the "io.sockets.on('connection', function (socket) {" inside the IF statement:

req.on('end', function() {
    var decodedBody = querystring.parse(fullBody);
    console.log(decodedBody);

    io.sockets.on('connection', function (socket) {
        socket.emit('user-aaa5c8bbffe4db9', fullBody);
    });
});

But that did not produce "instant" results, the notifications would come in to the client after a page refresh.

Client is a simple:

socket.on('user-<?php echo $_SESSION['user_display_id']; ?>', function (data) {
    alert(data);
    $('#events').html(data);
});

Any help would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-12-08 10:53:57

回答你的第一个问题。不,JavaScript 中没有global。定义该函数的范围内的所有变量都将在该函数中可用。

http://javascriptweblog.wordpress.com/2010/10/25/理解-javascript-闭包/

To answer your first question. No there is no global in JavaScript. All the variables that are in scope where the function is defined will be available in that function.

http://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/

无力看清 2024-12-08 10:53:57

在node.js中有对象global

您可以为其属性分配一些值,并且它们可以在任何地方访问,但我认为这不是最佳实践。

In node.js there's the object global.

You can assign to its property some values and they will be accesible everywhere, but I think it is not best practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文