2

I don't know when I should store something in the "socket session for the client". in the snippet below we use socket.username. I've seen other code that uses socket.id and that gets the id of a socket i think. Is there a list of properties to use on this socket object. right now I'm thinking that socket is an object and that we can add a new property with the dot method(operator), in this case the made up property is username and the object is socket . and we get the username from socket.emit('add user', $usernameInput.val().trim()); from the client script.

socket.on('add user', 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] = username;

Since I'm not sure how to use this socket ?object? Im not sure if I need to use it for this small "game" I'm trying to make. basically there's a canvas on the client and its split up into 4 quadrants and if player1 clicks on the "top right", the top right quadrant on the client(player1) and players 2 canvas is filled. and the player go back and forth clicking quadrant filling up the canvas I get the quadrant info by doing something like

        function checkCoord(left,top, right, bottom){
            //x and y are retreived on mousemove
            if(x < right && y < bottom){
                return "topLeft"
            }
             if( x > left && y <bottom ){
                return "top right"
            }

        }

Do I need to do something like socket.player in the index.js file? if so how would i do it

would this keep track of which player clicks on the quadrant?

After writing the above I looked over the code again I see

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

does this mean that when the socke.io code from the server(module) hears a connetion event we do some callback that has a socket object? and now we could do something with the socket object?

sorry this is so long. just want to learn how to use this socket stuff.

jack blank
  • 5,073
  • 7
  • 41
  • 73

1 Answers1

0

socket is the socket.io socket object passed to each connection received by your server. It's similar to the response object from httpServer in that it provides an interface for you to deal with the network connection/request.

The two main methods of the socket object that you'll use the most are:

socket.on(message_name, callback)
  // used to listen to messages from clients

socket.emit(message_name, anything)
  // used to send strings, numbers, objects or arrays to the client

For a full documentation of the socket object see: https://github.com/socketio/socket.io#socket

or: https://github.com/socketio/socket.io#socket

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • thanks for your answer Iv been using io.emit() and socket.on(). i hears that io is good for broadcasting to everyone in the chat room. why should I use `socket.emit` over `io.emit` ? which one is better? my connection looks like `io.on("connection", function(socket){` – jack blank Aug 07 '15 at 00:59
  • Use `io.emit` if you want to broadcast. Use `socket.emit` if you don't want to broadcast. The socket is connected to just one client. You'll have lots of `socket`s in your running app each one created when the callback to `io.on('connection')` is called. – slebetman Aug 07 '15 at 03:08
  • thank for your reply. what's the difference between broadcasting and sending. in the answer, your code example says to use `socket.emit()` "to send strings, numbers, etc.." but in your last comment you said to use socket.emit if you don't want to broadcast I thought sending data and broadcasting where the same. thanks for your help. – jack blank Aug 07 '15 at 05:19
  • broadcast = send to everyone, send = send. – slebetman Aug 07 '15 at 08:03
  • thanks for your answer im still not sure how to obtain information from a particular socket. I have another question that you could look at and answer to try and help me understand. thanks http://stackoverflow.com/questions/31911606/how-can-you-tell-which-socket-connection-clicked-a-button-using-socket-io – jack blank Aug 10 '15 at 05:10