Dedicated Server

setting up a multiplayer game environment using socket connection in android and node.js

You may download the complete project source code here



First of all create an new android studio project. Now right click the project from project explorer window and goto the project location by clicking "reveal in finder". Insider your project directory, create a new folder "server".

Now right click on the server folder and click "New terminal at Folder" or just open up a new terminal window and cd down to your "server" folder location

Now that you have terminal window opened for your server folder, type the command npm init and hit enter

npm init 

npm init command

Now you will be made to enter the project specific details in which you will need to enter details as shown above (just use the default values for all fields). Click "enter" and then type "yes" input to the dialog "is that OK". Now that you have successfully generated the package.json file, if you goto your server directory you can see that the package.json file has been generated.

Our initial setup for project is now done.
Now we will need to install socket.io by typing the following command and hitting enter


npm install socket.io --save
Here you can see that , the above command adds the modules to package.json file

Similarly, we will need to install express also


npm install express --save
Now we can jump into some coding part, first of all create a new file inside "server" directory named index.js and add the following code to it


The following code makes the node server to listen for events on port 8080

server.listen(8080,function()
{
console.log("Server is now running...");
});
so when our android client makes a request to port 8080, the server can respond accordingly. This will be covered in detail in upcoming sections.

The 'connection' event indicates that whenever a client makes a connection to the server, the server logs 'Player Connected!' along with the client id. Also whenever a player disconnects, the 'Player Disconnected!' message will be logged.

 now open up your android studio, you will have the following project structure
server directory present inside the android project directory

From now on, we can use android studio's terminal window for invoking our server. First of all click in terminal option in the bottom and "cd" down to server directory. Run the following commands

  • cd server
  • node index.js
running the server

If everything goes well, you can see the message "Server is running...". Please be sure that you get this message correctly in order to proceed further.

Now we will look into some client side code in android, so go to your android app's app level build.gradle file and add the following dependency


compile "io.socket:socket.io-client:0.6.2"

Now go to your AndroidManifest.xml file and add the internet permission as shown below :


Now go to your app's MainActivity.java file and add the following code to it



socket = IO.socket("http://192.168.1.2:8080"); 

also make sure that you declare with the right socket io package like this

import io.socket.client


here you should replace above ip with your ip address and keep the port as 8080 itself, you can use ipconfig to find your ip if you are using windows or in mac you can go to system prefernces >> network

Now make sure that your server is running and is showing the message "Server is running..", and then run the android studio project

As soon as the app starts running in your emulator/device, check the android studio terminal window, it should display something like "Player Connected!ABoIjzSQIeXSD2VZAAAA". It shows that player with specific id has joined. See below image


player connected

Now if you close/quit your android app, the server should output "Player Disconnected!" like shown below


player disconnected

2 comments:

  1. Thanks, this is a nice starter tut

    ReplyDelete
    Replies
    1. glad it helped, do checkout here for more android tutorials -- https://www.youtube.com/channel/UCgGL8Wwaf-mYRB2uNZYYl-g?sub_confirmation=1

      Delete