Gottox socket.io-java-client“握手时出错引起:java.io.FileNotFoundException:http://192.168.1.69:8080 / socket.io / 1

时间:2014-06-06 11:50:17

标签: java android node.js socket.io

我正在尝试使用 android app 简单节点js(socket.io)服务器进行通信我正在使用 Gottox socket.io-java-client lib 转换为jar文件作为客户端但它总是返回错误(找不到握手文件时出错)我在本地网络上的移动设备上使用wifi测试应用程序我的ip是192.168.1.69 我已通过网络测试了服务器通信,它运行正常

节点js套接字服务器代码

var mongo = require('mongodb').MongoClient,
    Client = require('socket.io').listen(8080).sockets;
    mongo.connect('mongodb://192.168.1.69/chat',function(err, db){
            if(err) throw err;
            Client.on('connection',function(socket){
                    //get mongo db collection
                    console.log("Connected to server");
                    var col = db.collection("messages"),
                    sendStatus = function(s){
                            socket.emit('status',s);
                        };
                    //emit all messages
                    col.find().limit(100).sort({_id:1}).toArray(function(err, res){
                            if(err) throw err;
                            socket.emit("output",res);
                        }); 

                    //wait for input
                    socket.on('input',function(data){
                            console.log("data-------> name: "+data.name+" message: "+data.message);
                            //insert into mongodb server
                            //json data from client
                            var name = data.name,
                                message = data.message,
                                whitespacePattern = /^\s*$/;
                                if(whitespacePattern.test(name) || whitespacePattern.test(message)){
                                    sendStatus("Name and Message is required.");
                                    console.log("invalid data");
                                }else{
                                      col.insert({name: name,message:message},function(){
                                          //emit latest to all clients 
                                          Client.emit('output',[data]);
                                        sendStatus({
                                                message:"message sent",
                                                clear:true
                                            });
                                      });                                            
                                    }
                        //on input
                        });
                //on connection
                });
        //mongo db connect
        });

Android客户端代码

package com.example.socketio;

import java.net.MalformedURLException;

import org.json.JSONObject;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView2);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    SocketIO socket = new SocketIO("http://192.168.1.69:8080");
                    socket.connect(new IOCallback() {

                        @Override
                        public void onMessage(JSONObject arg0, IOAcknowledge arg1) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onMessage(String arg0, IOAcknowledge arg1) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onError(SocketIOException arg0) {
                            // TODO Auto-generated method stub
                            arg0.printStackTrace();
                        }

                        @Override
                        public void onDisconnect() {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onConnect() {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void on(String arg0, IOAcknowledge arg1, Object... arg2) {
                            // TODO Auto-generated method stub

                        }
                    });
                socket.emit("input", "hello");  
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });

    }

}

错误:

06-07 11:05:35.414: W/System.err(26605): io.socket.SocketIOException: Error while handshaking
06-07 11:05:35.414: W/System.err(26605):    at io.socket.IOConnection.handshake(IOConnection.java:322)
06-07 11:05:35.414: W/System.err(26605):    at io.socket.IOConnection.access$600(IOConnection.java:39)
06-07 11:05:35.424: W/System.err(26605):    at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
06-07 11:05:35.424: W/System.err(26605): Caused by: java.io.FileNotFoundException: http://192.168.1.69:8080/socket.io/1/
06-07 11:05:35.424: W/System.err(26605):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
06-07 11:05:35.424: W/System.err(26605):    at io.socket.IOConnection.handshake(IOConnection.java:313)
06-07 11:05:35.424: W/System.err(26605):    ... 2 more

2 个答案:

答案 0 :(得分:4)

问题解决了(某种程度上),这里:Android developpement, Gottox socket.io-java-client: file not fount Exception /socket.io/1/

(尝试使用早期版本的socket.io - 首先从node_modules删除socket.io文件夹,然后使用以下命令安装旧版本,例如0.9.16:npm install socket.io@0.9.16)

答案 1 :(得分:2)

使用这个新库来开始使用Java上的socket.io 1.0

https://github.com/nkzawa/socket.io-client.java/blob/master/README.md