android,node.js code included free source code zip download

npm init

npm init command

socket.io



npm install socket.io --save



npm install express --save

server

index.js



server.listen(8080,function()

{

console.log("Server is now running...");

});

server directory present inside the android project directory

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"

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

import io.socket.client

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





You may download the complete project source code hereFirst 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 locationNow that you have terminal window opened for your server folder, type the command npm init and hit enterNow 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 installby typing the following command and hitting enterHere you can see that , the above command adds the modules to package.json fileSimilarly, we will need to install express alsoNow we can jump into some coding part, first of all create a new file inside "" directory namedand add the following code to itThe following code makes the node server to listen for events on port 8080so 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 structureFrom 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 commandsNow 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 italso make sure that you declare with the right socket io package like thishere 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