gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:17:13 27 4
gpt4 key购买 nike

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

node js socket server代码

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 - Gottox socket.io-java-client “握手时出错,由 : java. io.FileNotFoundException : http://192. 168.1.69 :8080/socket. io/1/引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com