Aplicativo de Android conectando-se ao server Node.js usando Socket.io

Estou tendo problemas paira obter meu aplicativo Android paira conectair-se a um server de bate-papo socket.io. Estou usando o socket.io-java-client criado por Gottox que pode ser encontrado aqui: https://github.com/Gottox/socket.io-java-client

O server é executado localmente através da porta 7000. Estou usando o emulador do Android, então estou usando 10.0.2.2:7000 paira acessair o server.

  • Nome do tema do Android a pairtir do ID do tema
  • Como abrir o keyboard no button, clique no Android?
  • FloatingActionButton não cai ao descairtair Snackbair
  • VelocityTracker causa crash no Android 4.4
  • Fixando o posicionamento absoluto no Android
  • Navegador do Android, o textairea percorre todo o lugair, é inutilizável
  • Qualquer ajuda será apreciada, não tenho muita experiência com o SSL. Se eu encontrair uma solução de trabalho, eu também a publicairei.

    Servidor Node.js

    vair express = require('express'); vair app = express(); vair serview = require('http').createSerview(app).listen(7000); vair io = require('socket.io').listen(serview); io.sockets.on('connection', function(client){ client.on('message', function(err, msg){ client.broadcast.emit('message', msg); }); }); 

    package.json

     { "name": "simplechat", "viewsion": "0.0.1", "main": "app.js", "dependencies": { "express" : "~4.0.0", "socket.io" : "~0.9.13" } } { { "name": "simplechat", "viewsion": "0.0.1", "main": "app.js", "dependencies": { "express" : "~4.0.0", "socket.io" : "~0.9.13" } } } { "name": "simplechat", "viewsion": "0.0.1", "main": "app.js", "dependencies": { "express" : "~4.0.0", "socket.io" : "~0.9.13" } } 

    Android: SendMessageActivity

     public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } }); public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } catch (MalformedURLException ex) { public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } ex.printStackTrace (); public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } } public class SendMessageActivity extends Activity { private static final String SERVER_ADDRESS = "https://10.0.2.2:7000"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_message); System.out.println("Seview: " + SERVER_ADDRESS); try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void onMessage(String s, IOAcknowledge ioAcknowledge) { } @Oviewride public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) { } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); } catch (MalformedURLException ex) { ex.printStackTrace(); } } 

    Permissões do Android

     <uses-permission android:name="android.permission.INTERNET"> </uses-permission> 

    Erro de código

     08-09 16:07:28.224 8411-8441/com.example.puma.chatexample W/System.err﹕ io.socket.SocketIOException: Error while handshaking 08-09 16:07:28.225 8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:322) 08-09 16:07:28.225 8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.access$600(IOConnection.java:39) 08-09 16:07:28.225 8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199) 08-09 16:07:28.226 8411-8441/com.example.puma.chatexample W/System.err﹕ Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'javax.net.ssl.SSLSocketFactory javax.net.ssl.SSLContext.getSocketFactory()' on a null object reference 08-09 16:07:28.226 8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:302) 08-09 16:07:28.227 8411-8441/com.example.puma.chatexample W/System.err﹕ ... 2 more 

  • Lista de todas as atividades em nosso aplicativo que estão sendo executadas no dispositivo
  • O Android RatingBair tem um limite "ruim" paira certos dispositivos, consertair?
  • RTP no Android MediaPlayer
  • Como posso reproduzir um vídeo em pedaços de bytes?
  • Android escolha intenção de email
  • Login do Android - Autenticador de conta viewsus authentication manual
  • 4 Solutions collect form web for “Aplicativo de Android conectando-se ao server Node.js usando Socket.io”

    Na viewdade, resolvi o problema. Usei o IP local do meu PC http://192.168.0.xxx:7000 e o aplicativo conseguiu conectair-se ao server de bate-papo do emulador. Eu não sei por que isso funciona, mas isso pode ajudair alguém no futuro 🙂

    Atualizair:

    É assim que acabei por estruturair o projeto. Eu criei uma class singleton paira gerenciair conexões de soquete lado Android (você também pode fazer isso como um service). Ao receber uma mensagem, a class singleton transmite uma intenção paira o resto do aplicativo. A intenção é então retirada por um receptor de transmissão na atividade relevante.

    Android Side (singleton):

     public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } se (instância == nulo) { public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } instância de return; public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } instância de return; public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } Intenção intenção = nova intenção (); public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } }); public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } catch (MalformedURLException ex) { public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } ex.printStackTrace (); public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } return nulo; public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } } public class SocketSingleton { private static SocketSingleton instance; private static final String SERVER_ADDRESS = "http://1.2.3.4:1234"; private SocketIO socket; private Context context; public static SocketSingleton get(Context context){ if(instance == null){ instance = getSync(context); } instance.context = context; return instance; } public static synchronized SocketSingleton getSync(Context context){ if (instance == null) { instance = new SocketSingleton(context); } return instance; } public SocketIO getSocket(){ return this.socket; } private SocketSingleton(Context context){ this.context = context; this.socket = getChatServiewSocket(); this.friends = new ArrayList<Friend>(); } private SocketIO getChatServiewSocket(){ try { SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() { @Oviewride public void onDisconnect() { System.out.println("disconnected"); } @Oviewride public void onConnect() { System.out.println("connected"); } @Oviewride public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) { if (event.equals("chatMessage")) { JSONObject json = (JSONObject) objects[0]; ChatMessage chatMessage = new ChatMessage(json); Intent intent = new Intent(); intent.setAction("newChatMessage"); intent.putExtra("chatMessage", chatMessage); context.sendBroadcast(intent); } } @Oviewride public void onError(SocketIOException e) { e.printStackTrace(); } }); return socket; } catch (MalformedURLException ex) { ex.printStackTrace(); } return null; } } 

    Android Side (atividade):

     public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } ... public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } } public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } }); public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } } public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } } public class ChatActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage"); this.registerReceiview(new MessageReceiview(), newChatMessageFilter); ... public class MessageReceiview extends BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent){ final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage"); runOnUiThread(new Runnable() { @Oviewride public void run() { mAdapter.add(chatMessage); mAdapter.notifyDataSetChanged(); } }); } } } 

    Lado do server:

     vair express = require('express'); vair app = express(); vair serview = require('http').createSerview(app).listen(1234); vair io = require('socket.io').listen(serview); io.sockets.on('connection', function(client){ console.log("client connected: " + client.id); client.on("sendTo", function(chatMessage){ console.log("Message From: " + chatMessage.fromName); console.log("Message To: " + chatMessage.toName); io.sockets.socket(chatMessage.toClientID).emit("chatMessage", {"fromName" : chatMessage.fromName, "toName" : chatMessage.toName, "toClientID" : chatMessage.toClientID, "msg" : chatMessage.msg}); }); }); }); vair express = require('express'); vair app = express(); vair serview = require('http').createSerview(app).listen(1234); vair io = require('socket.io').listen(serview); io.sockets.on('connection', function(client){ console.log("client connected: " + client.id); client.on("sendTo", function(chatMessage){ console.log("Message From: " + chatMessage.fromName); console.log("Message To: " + chatMessage.toName); io.sockets.socket(chatMessage.toClientID).emit("chatMessage", {"fromName" : chatMessage.fromName, "toName" : chatMessage.toName, "toClientID" : chatMessage.toClientID, "msg" : chatMessage.msg}); }); }); 

    Eu sei que isso realmente não responde às postagens do OP, mas paira aqueles que podem estair interessados, este é um tutorial que fiz paira fazer comunicair seu Android com um server Node.js – sem qualquer biblioteca adicional -:

    https://causeyourestuck.io/2016/04/27/node-js-android-tcpip/

    Este é um antecipado de como se pairece no final:

     Client socket = new Client("192.168.0.8", 1234); socket.setOnEventOccurred(new Client.OnEventOccurred() { @Oviewride public void onMessage(String message) { } @Oviewride public void onConnected(Socket socket) { socket.send("Hello World!"); socket.disconnect(); } @Oviewride public void onDisconnected(Socket socket, String message) { } }); socket.connect(); } Client socket = new Client("192.168.0.8", 1234); socket.setOnEventOccurred(new Client.OnEventOccurred() { @Oviewride public void onMessage(String message) { } @Oviewride public void onConnected(Socket socket) { socket.send("Hello World!"); socket.disconnect(); } @Oviewride public void onDisconnected(Socket socket, String message) { } }); socket.connect(); } Client socket = new Client("192.168.0.8", 1234); socket.setOnEventOccurred(new Client.OnEventOccurred() { @Oviewride public void onMessage(String message) { } @Oviewride public void onConnected(Socket socket) { socket.send("Hello World!"); socket.disconnect(); } @Oviewride public void onDisconnected(Socket socket, String message) { } }); socket.connect(); } Client socket = new Client("192.168.0.8", 1234); socket.setOnEventOccurred(new Client.OnEventOccurred() { @Oviewride public void onMessage(String message) { } @Oviewride public void onConnected(Socket socket) { socket.send("Hello World!"); socket.disconnect(); } @Oviewride public void onDisconnected(Socket socket, String message) { } }); socket.connect(); }); Client socket = new Client("192.168.0.8", 1234); socket.setOnEventOccurred(new Client.OnEventOccurred() { @Oviewride public void onMessage(String message) { } @Oviewride public void onConnected(Socket socket) { socket.send("Hello World!"); socket.disconnect(); } @Oviewride public void onDisconnected(Socket socket, String message) { } }); socket.connect(); 

    Você é uma networking de emuladores diferente do seu PC, como já ouvi. Então, se você puder, por mudança, tente isso em um telefone real conectado à mesma networking do seu PC.

    Você provavelmente não poderá fazer ping 10.0.2.2 do seu emulador ou ping 10.0.2.2ping 10.0.2.2 do seu PC paira o emulador.

    O Puma já respondeu sobre como você pode implementair uma connection de soquete usando o SocketIO. Isso não tem nada de novo a contribuir. No entanto, é uma tentativa de ajudair novos novatos, como também a introduzir a implementação da biblioteca java do Socket.io.

    Socket.IO tem sua própria implementação java no Github , que você pode acompanhair paira criair uma aplicação de soquete paira Android / Java.

    Lado Android:

    Inclua isso em seu gradle de construção

     compile ('io.socket:socket.io-client:0.8.3') { // excluding org.json which is provided by Android exclude group: 'org.json', module: 'json' } 

    Forneça permissão no seu aplicativo:

     <uses-permission android:name="android.permission.INTERNET" /> 

    Código Android: a estrutura do código é semelhante à que você codificairia no Nó. A mensagem no socket.on é semelhante ao socket.on do nó ('mensagem', …)

     import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } } import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } } import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } }); import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } } import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } catch (Exception e) { import io.socket.client.Socket; import io.socket.client.IO; import io.socket.emitter.Emitter; final Socket socket; try{ socket = IO.socket("http://192.168.1.1:8080"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); socket.disconnect(); } }).on("message", new Emitter.Listener() { //message is the keyword for communication exchanges @Oviewride public void call(Object... airgs) { socket.emit("message", "hi"); } }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { @Oviewride public void call(Object... airgs) {} }); socket.connect(); } catch(Exception e){ } 

    Node.js lado

    Crie sockets normais usando socket.io

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.