1
votes

I get no error when running app.js but I can't get a JavaScript prompt for username. I can only load chat.html everything else doesn't work. I think the problem might be:

io = require('socket.io').listen(server); or

server.listen(80);

Also in network tab i get: ?t=1394734845750 /socket.io/1 GET (failed) net::ERR_CONNECTION_REFUSED socket.io.js:1659 in red color.

app.js

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(80);

var fs = require('fs');


// routing
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/chat.html');
});

// usernames which are currently connected to the chat
var usernames = {};

function check_key(v)
{
    var val = '';

    for(var key in usernames)
    {
        if(usernames[key] == v)
        val = key;
    }
    return val;
}

io.sockets.on('connection', function (socket) {

    // when the client emits 'sendchat', this listens and executes
    socket.on('sendchat', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.emit('updatechat', socket.username, data);
    });

    // when the client emits 'adduser', this listens and executes
    socket.on('adduser', function(username){
        // we store the username in the socket session for this client
        socket.username = username;
        // add the client's username to the global list
        usernames[username] = socket.id;
        // echo to client they've connected
        socket.emit('updatechat', 'SERVER', 'you have connected');
        // echo to client their username
        socket.emit('store_username', username);
        // echo globally (all clients) that a person has connected
        socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected: ' + socket.id);
        // update the list of users in chat, client-side
        io.sockets.emit('updateusers', usernames);
    });

    // when the user disconnects.. perform this
    socket.on('disconnect', function(){
        // remove the username from global usernames list
        delete usernames[socket.username];
        // update list of users in chat, client-side
        io.sockets.emit('updateusers', usernames);
        // echo globally that this client has left
        socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
    });

    // when the user sends a private msg to a user id, first find the username
    socket.on('check_user', function(asker, id){
        //console.log("SEE: "+asker); console.log(id);
        io.sockets.socket(usernames[asker]).emit('msg_user_found', check_key(id));
    });

    // when the user sends a private message to a user.. perform this
    socket.on('msg_user', function(usr, username, msg) {
        //console.log("From user: "+username);
        //console.log("To user: "+usr);
        //console.log(usernames);
        io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg);

        fs.writeFile("chat_data.txt", msg, function(err) {
            if(err) {
            console.log(err);
            } /*else {
            console.log("The file was saved!");
            }*/
            });
    });


});

chat.html

<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
var my_username = '';
function send_individual_msg(id)
{
    //alert(id);
    //alert(my_username);
    socket.emit('check_user', my_username, id);
    //socket.emit('msg_user', id, my_username, prompt("Type your message:"));
}

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

    // on connection to server, ask for user's name with an anonymous callback
    socket.on('connect', function(){
        // call the server-side function 'adduser' and send one parameter (value of prompt)
        socket.emit('adduser', prompt("What's your name?"));
    });

    // listener, whenever the server emits 'msg_user_handle', this updates the chat body
    socket.on('msg_user_handle', function (username, data) {
        $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');           
    });

    // listener, whenever the server emits 'msg_user_found'
    socket.on('msg_user_found', function (username) {
        //alert(username);
        socket.emit('msg_user', username, my_username, prompt("Type your message:"));
    });

    // listener, whenever the server emits 'updatechat', this updates the chat body
    socket.on('updatechat', function (username, data) {
        $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
    });

    // listener, whenever the server emits 'store_username', this updates the username
    socket.on('store_username', function (username) {
        my_username = username;
    }); 

    // listener, whenever the server emits 'updateusers', this updates the username list
    socket.on('updateusers', function(data) {
        //alert(data);
        //console.log(data);
        $('#users').empty();
        $.each(data, function(key, value) {
            $('#users').append('<div style="cursor:pointer;" onclick="send_individual_msg(\''+value+'\')">' + key + '</div>');
        });
    });

    // on load of page
    $(function(){
        // when the client clicks SEND
        $('#datasend').click( function() {
            var message = $('#data').val();
            if(message == '' || jQuery.trim(message).length == 0)
            return false;
            $('#data').val('');
            // tell server to execute 'sendchat' and send along one parameter
            socket.emit('sendchat', message);
        });

        // when the client hits ENTER on their keyboard
        $('#data').keypress(function(e) {
            if(e.which == 13) {
                $(this).blur();
                //$('#datasend').focus().click();
                $('#datasend').click();
            }
        });
    });

</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
    <b>USERS</b>
    <div id="users"></div>
</div>
<div style="float:left;width:550px;height:250px;overflow:scroll-y;padding:10px;">
    <div id="conversation"></div>
    <input id="data" style="width:200px;" />
    <input type="button" id="datasend" value="send" />
</div>
1

1 Answers

1
votes

Your server is listening on port 80, but your socket is connecting to port 8008. Change one of these so that they are on the same port.