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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gottox socket.io-java-client“握手时出错引起:java.io.FileNotFoundException:http://192.168.1.69:8080 / socket相关的知识,希望对你有一定的参考价值。

我试图与我的android应用程序沟通简单的节点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
答案

问题解决了(某种程度上),这里: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)

另一答案

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

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

以上是关于Gottox socket.io-java-client“握手时出错引起:java.io.FileNotFoundException:http://192.168.1.69:8080 / socket的主要内容,如果未能解决你的问题,请参考以下文章

Gottox socket.io-java-client“握手时出错”空指针异常

Gottox socket.io-java-client“握手时出错原因:java.io.FileNotFoundException:http://192.168.1.69:8080/socket.i

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

必须在不同的活动中调用单例 socketIO 类?

如何在android上使用socket.io和后台服务?