简体   繁体   中英

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.

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

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

any hints/ideas on what I might be doing wrong.

thank-you in advance -- Vinny

Also it seem that you are including server side socket.io code. 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. A solution for this is to send the disconnected event from the client side. There are events that indicates a user leaves a page such as the onbeforeunload event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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