简体   繁体   English

node.js socket.io页面查看器

[英]node.js socket.io page viewers

I want to use node.js and socket.io to show how many users are currently viewing a certain page on my website, by sending a message with the page address (ie /homepage or /events) to the node.js server, then read/write a file with the address name (ie homepage.txt, or events.txt), get the current count and add 1 then write it back to the file, then do the same on disconnect but instead remove 1 and save it. 我想通过将带有页面地址(即/ homepage或/ events)的消息发送到node.js服务器,以使用node.js和socket.io来显示当前有多少用户正在查看我网站上的某个页面,然后读取/写入具有地址名称的文件(即homepage.txt或events.txt),获取当前计数并加1,然后将其写回到文件中,然后在断开连接时执行相同操作,但删除1并保存。

Here is the code I have so far, currently excluding the fs functions for testing, also the server file and client file are in the same directory 这是我到目前为止的代码,当前不包括用于测试的fs函数,服务器文件和客户端文件也位于同一目录中

Client: 客户:

<?php
    $url = $_SERVER["REQUEST_URI"];
?>

<body style="overflow: hidden;" />

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="/node_modules/socket.io/lib/socket.io.js"></script>

<script>

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

    socket.on('connected', function (data) {
        $("#log").append('Connected!<br />');
        $("#log").append(data + "<br />");
        socket.emit('viewing', { page: '<?php echo $url; ?>' });
            socket.on('disconnected', function(data) {
            $("#log").append("User Disconnected" + "<br />");
        });
    });

</script>

<div id='log' style='height: 100%; border: 1px solid black;'></div>

Server: 服务器:

var io = require('socket.io').listen(5000);

var connected_users = 0;

io.sockets.on('connection', function(socket) {
    connected_users++;
    console.log("User Connected!");
    socket.emit("connected", { message: "Users Online: " + connected_users + "!" });

    socket.on('viewing', function(data){
        console.log("User Is Viewing: " + data);
    });

    socket.on('disconnect', function(){
        connected_users--;
        console.log("User Disconnected!");
        socket.emit("disconnected", { message: "Users Online: " + connected_users + "!" });
    });
});

but for some reason this doesn't seem to be working, all the server says is "info - socket.io started" and nothing else when a user connects to the client page 但是由于某种原因,这似乎不起作用,所有服务器都说“信息-socket.io已启动”,而当用户连接到客户端页面时则什么也没有

any hints/ideas on what I might be doing wrong. 关于我可能在做错的任何提示/想法。

thank-you in advance -- Vinny 在此先感谢您-Vinny

Also it seem that you are including server side socket.io code. 同样,您似乎包括了服务器端的socket.io代码。 Correct script tag would be this: 正确的脚本标签是这样的:

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

In the line you write, there are some problems in your code. 在您编写的行中,您的代码中存在一些问题。

socket.on('viewing', function(data){
    console.log("User Is Viewing: " + data);//it should be data.page
});

Also, when the disconnect event happened at the server side, the user has already left the page and the socket is closed so you can't send anything to the client. 另外,当服务器端发生disconnect事件时,用户已经离开页面并且套接字已关闭,因此您无法向客户端发送任何消息。 A solution for this is to send the disconnected event from the client side. 一种解决方案是从客户端发送disconnected事件。 There are events that indicates a user leaves a page such as the onbeforeunload event. 有些事件指示用户离开页面,例如onbeforeunload事件。

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

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