Successfully run Socket.IO Android Project with NodeJS
Your problem is that the node.js Server run on Localhost, and any device or emulator run on Virtual Machines, so, never listen the localhost of your PC. You need to change the URL
socket = new SocketIO("http://localhost:3000/");
For
Your Public or static IP of your machine in your network environment, for example:
socket = new SocketIO("http://192.168.1.23:3000/");
Best. C
Victor Diaz
Updated on October 21, 2022Comments
-
Victor Diaz over 1 year
I've managed to get a Socket.IO/Android project to compile and now I'm trying to get it to communicate with Socket.IO on a NodeJS server. I placed the project on my Drop Box here:
https://dl.dropboxusercontent.com/u/86164338/socketIOAndroid.zip
Everything compiles and runs but I see no output from NodeJS.
This is based on: https://github.com/Gottox/socket.io-java-client
Note that there are instructions on compiling the Java code and copying the jar file to the libs folder:
git clone git://github.com/Gottox/socket.io-java-client.git
cd socket.io-java-client
ant jar mv jar/socketio.jar
/path/to/your/libs/projectAny help is greatly appreciated.
Java Code:
package com.depictlabs.testsockets03; import java.net.MalformedURLException; import java.net.URI; import org.json.JSONObject; import io.socket.SocketIO; import io.socket.IOCallback; import io.socket.IOAcknowledge; import io.socket.SocketIOException; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SocketIO socket; try{ System.out.println("Initializing Connection."); socket = new SocketIO("http://localhost:3000/"); socket.connect ( new IOCallback(){ @Override public void onMessage(JSONObject json, IOAcknowledge ack) { try { System.out.println("Server said:" + json.toString(2)); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onMessage(String data, IOAcknowledge ack) { System.out.println("Server said: " + data); } @Override public void onError(SocketIOException socketIOException) { System.out.println("an Error occured"); socketIOException.printStackTrace(); } @Override public void onDisconnect() { System.out.println("Connection terminated."); } @Override public void onConnect() { System.out.println("Connection established"); } @Override public void on(String event, IOAcknowledge ack, Object... args) { System.out.println("Server triggered event '" + event + "'"); } } ); try { // This line is cached until the connection is established. System.out.println("Sending message to server."); socket.send("user message"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e1) { e1.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
NodeJS Code:
var http = require('http') , io = require('socket.io'); var app = http.createServer(); app.listen(3000); console.log('Server running at http://127.0.0.1:3000/'); // Socket.IO server var io = io.listen(app) , nicknames = {}; io.sockets.on('connection', function (socket) { socket.on('user message', function (msg) { console.log('Server running at http://127.0.0.1:3000/'); socket.broadcast.emit('user message', {user: socket.nickname, message: msg.message}); }); socket.on('nickname', function (nick, fn) { console.log('Server running at http://127.0.0.1:3000/'); nickname = nick.nickname; if (nicknames[nickname]) { fn(true); } else { fn(false); nicknames[nickname] = socket.nickname = nickname; socket.broadcast.emit('announcement', {user: nickname, action: 'connected'}); io.sockets.emit('nicknames', nicknames); } }); socket.on('disconnect', function () { console.log('Server running at http://127.0.0.1:3000/'); if (!socket.nickname) return; delete nicknames[socket.nickname]; socket.broadcast.emit('announcement', {user: socket.nickname, action: 'disconected'}); socket.broadcast.emit('nicknames', nicknames); }); });
-
Anshul Tyagi over 6 yearsEmulator can be the issue ? This URL is running fine on web browser of the emulator.