Android App使用Socket.io连接到Node.js服务器

时间:2014-08-09 21:21:12

标签: android node.js sockets ssl socket.io

我无法让我的Android应用连接到socket.io聊天服务器。我使用Gottox创建的socket.io-java-client,可以在这里找到:https://github.com/Gottox/socket.io-java-client

服务器通过端口7000在本地运行。我使用的是Android模拟器,所以我使用10.0.2.2:7000访问服务器。

任何帮助都会受到赞赏,我对SSL没有多少经验。如果我找到一个有效的解决方案,我也会发布它。

Node.js服务器

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

的package.json

{
  "name": "simplechat",
  "version": "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";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);

        System.out.println("Sever: " + SERVER_ADDRESS);

        try {
            SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() {
                @Override
                public void onDisconnect() {
                    System.out.println("disconnected");
                }

                @Override
                public void onConnect() {
                    System.out.println("connected");
                }

                @Override
                public void onMessage(String s, IOAcknowledge ioAcknowledge) {
                }

                @Override
                public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) {
                }

                @Override
                public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) {
                }

                @Override
                public void onError(SocketIOException e) {
                    e.printStackTrace();
                }
            });

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

Android权限

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

错误代码

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

4 个答案:

答案 0 :(得分:34)

我实际上解决了这个问题。我使用了我的PC的本地IP http://192.168.0.xxx:7000,该应用程序能够从模拟器连接到聊天服务器。我不知道为什么会这样,但它可能会帮助将来的某个人:)

<强>更新

这就是我最终构建项目的方式。我创建了一个单例类来处理Android端的套接字连接(你也可以将其作为服务来实现)。当收到消息时,单例类会向应用程序的其余部分广播一个意图。然后,相关活动中的广播接收者将采集该意图。

Android Side(单身):

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 = getChatServerSocket();
        this.friends = new ArrayList<Friend>();
    }

    private SocketIO getChatServerSocket(){
        try {
            SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() {
                @Override
                public void onDisconnect() {
                    System.out.println("disconnected");
                }

                @Override
                public void onConnect() {
                    System.out.println("connected");
                }

                @Override
                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);
                    }
                }
                @Override
                public void onError(SocketIOException e) {
                    e.printStackTrace();
                }
            });
            return socket;
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Android方面(活动):

public class ChatActivity extends Activity {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    IntentFilter newChatMessageFilter = new IntentFilter("newChatMessage");
    this.registerReceiver(new MessageReceiver(), newChatMessageFilter);

    ...

    public class MessageReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
            final ChatMessage chatMessage =(ChatMessage) intent.getExtras().get("chatMessage");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mAdapter.add(chatMessage);
                    mAdapter.notifyDataSetChanged();
                }
            });
        }
    }
} 

服务器端:

var express = require('express');
var app = express();
var server = require('http').createServer(app).listen(1234);
var io = require('socket.io').listen(server);

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});

    });
});

答案 1 :(得分:7)

我知道这不是OP的帖子的真正答案,但对于那些可能感兴趣的人来说,这是我用来与Node.js服务器沟通你的Android的教程 - 没有任何额外的图书馆 - :

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

这是最终结果的预示:

Client socket = new Client("192.168.0.8", 1234);
socket.setOnEventOccurred(new Client.OnEventOccurred() {
    @Override
    public void onMessage(String message) {
    }

    @Override
    public void onConnected(Socket socket) {
        socket.send("Hello World!");
        socket.disconnect();
    }

    @Override
    public void onDisconnected(Socket socket, String message) {
    }
});

socket.connect();

答案 2 :(得分:4)

Puma已经回答了如何使用SocketIO实现套接字连接。这没有什么新的贡献。然而,它是一个帮助新手的尝试,同时也介绍了Socket.io的java库的实现。

Socket.IO在Github上有自己的java实现,您可以按照它来创建Android / Java的套接字应用程序。

Android方面:

在您的构建 gradle

中包含此内容
compile ('io.socket:socket.io-client:0.8.3') {
    // excluding org.json which is provided by Android
    exclude group: 'org.json', module: 'json'
}
在您的应用中

提供权限

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

Android代码: 代码结构类似于您在Node中编码的方式。 socket.on中的消息类似于节点的socket.on('message',...)

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() {

    @Override
    public void call(Object... args) {
        socket.emit("message", "hi");
        socket.disconnect();
    }

}).on("message", new Emitter.Listener() {
        //message is the keyword for communication exchanges
    @Override
    public void call(Object... args) {
        socket.emit("message", "hi");
    }

}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

    @Override
    public void call(Object... args) {}

});
    socket.connect();

}
catch(Exception e){

}

Node.js side

使用socket.io

创建普通套接字

答案 3 :(得分:3)

正如我所听到的,您的模拟器网络与您的PC不同。因此,如果您可以通过更改在与PC连接到同一网络的实际手机上尝试此操作。

您可能无法从模拟器ping 10.0.2.2或从PC到仿真器的其他方式。