Android 开发,Gotox socket.io-java-client: 文件不是源异常/socket.io/1/ [英] Android developpement, Gottox socket.io-java-client: file not fount Exception /socket.io/1/
问题描述
这是我的问题:
我一直在尝试使用 socket.io-java-client对于 android 应用程序,但我最终出现了握手错误(见下文).
这是我的应用源代码:
public void runIO(){尝试 {SocketIO socket = new SocketIO("http://192.168.1.60:1337");socket.connect(new IOCallback() {@覆盖公共无效onMessage(JSONObject json,IOAcknowledge ack){尝试 {System.out.println("服务器说:" + json.toString(2));} catch (JSONException e) {e.printStackTrace();}}@覆盖public void onMessage(字符串数据,IOAcknowledge ack){System.out.println("服务器说:" + 数据);}@覆盖public void onError(SocketIOException socketIOException) {System.out.println("发生错误");socketIOException.printStackTrace();}@覆盖公共无效 onDisconnect() {System.out.println("连接终止.");}@覆盖公共无效 onConnect() {System.out.println("连接建立");}@覆盖public void on(String event, IOAcknowledge ack, Object... args) {System.out.println("服务器触发事件'" + event + "'");}});//该行被缓存,直到连接建立.socket.send("你好服务器!");} catch (MalformedURLException e) {//TODO 自动生成的 catch 块e.printStackTrace();}}
和我的 Node.js/Socket.io:
var app = require('http').createServer(handler)var io = require('socket.io')(app);var fs = require('fs');应用程序听(1337);函数处理程序 (req, res) {fs.readFile(__dirname + '/index.html',功能(错误,数据){如果(错误){res.writeHead(500);return res.end('加载 index.html 时出错');}res.writeHead(200);res.end(数据);});}io.on('connection', function (socket) {socket.emit('news', { 你好: 'world' });socket.on('我的另一个事件',函数(数据){控制台日志(数据);});});
我收到以下错误:
06-09 03:59:53.955: W/System.err(11738): io.socket.SocketIOException: 握手时出错06-09 03:59:53.965:W/System.err(11738):在io.socket.IOConnection.handshake(IOConnection.java:322)06-09 03:59:53.965:W/System.err(11738):在io.socket.IOConnection.access$7(IOConnection.java:292)06-09 03:59:53.970: W/System.err(11738): 在 io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)06-09 03:59:53.970:W/System.err(11738):引起:java.io.FileNotFoundException:http://192.168.1.60:1337/socket.io/1/06-09 03:59:53.975:W/System.err(11738):在 libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)06-09 03:59:53.975:W/System.err(11738):在io.socket.IOConnection.handshake(IOConnection.java:313)
在浏览器上它可以工作,并且位于 http://192.168.1.60:1337/socket.io/1/
的页面返回以下消息(通过浏览器):
{"code":0,"message":"传输未知"}
我不知道该怎么做才能让它工作......请帮忙:x
谢谢:D
正如Larky 所说,我不得不将我的 Socket.io 版本降级到 0.9.16.使用以下节点代码,它就像一个魅力:
var io = require('socket.io').listen(80);io.on('connection', function (socket) {socket.emit('news', { 你好: 'world' });socket.on('我的另一个事件',函数(数据){控制台日志(数据);});});
我今天通过在服务器端恢复到较早版本的 socket.io(较早版本的 socket.io Node 模块)解决了这个问题.也许协议改变了?
要做到这一点:
删除 node_modules 文件夹中的 socket.io 文件夹.
在控制台输入:
<块引用>
npm install socket.io@0.9.16
应该可以...它对我有用:)
(显然这并不能完全解决问题,但较早版本的 socket.io 对我来说很好用)
here is my problem:
i've been trying to use socket.io-java-client for an android app but i'm ending up with an handshake error (see below).
here is my app source code:
public void runIO(){
try {
SocketIO socket = new SocketIO("http://192.168.1.60:1337");
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and my Node.js / Socket.io:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(1337);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
And i'm getting the following error:
06-09 03:59:53.955: W/System.err(11738): io.socket.SocketIOException: Error while handshaking
06-09 03:59:53.965: W/System.err(11738): at io.socket.IOConnection.handshake(IOConnection.java:322)
06-09 03:59:53.965: W/System.err(11738): at io.socket.IOConnection.access$7(IOConnection.java:292)
06-09 03:59:53.970: W/System.err(11738): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
06-09 03:59:53.970: W/System.err(11738): Caused by: java.io.FileNotFoundException: http://192.168.1.60:1337/socket.io/1/
06-09 03:59:53.975: W/System.err(11738): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
06-09 03:59:53.975: W/System.err(11738): at io.socket.IOConnection.handshake(IOConnection.java:313)
On a browser it works, and the page at http://192.168.1.60:1337/socket.io/1/
is returning the following message (trough a browser):
{"code":0,"message":"Transport unknown"}
i don't know what to do to make it work ... please help :x
Thanks :D
EDIT:
As Larky said, i had to downgrade my version of Socket.io to 0.9.16. It worked like a charm with the following node code:
var io = require('socket.io').listen(80);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I solved the problem today by reverting to an earlier version of socket.io on the server-side (an earlier version of the socket.io Node module). Perhaps the protocol changed?
To do this:
Delete the socket.io folder in your node_modules folder.
On the console type:
npm install socket.io@0.9.16
That should do it... it worked for me :)
(obviously this doesn't completely solve the problem but the earlier version of socket.io worked fine for me for my purposes)
这篇关于Android 开发,Gotox socket.io-java-client: 文件不是源异常/socket.io/1/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!