下面是我的问题:
我一直在尝试使用 socket.io-Java的客户端对于一个Android应用程序但我结束了与握手错误(见下文)。
下面是我的应用程序源$ C $ C:
公共无效runIO(){
尝试 {
SocketIO插座=新SocketIO(http://192.168.1.60:1337);
socket.connect(新IOCallback(){
@覆盖
公共无效的onMessage(JSONObject的JSON,IOAcknowledge ACK){
尝试 {
的System.out.println(服务器说:+ json.toString(2));
}赶上(JSONException E){
e.printStackTrace();
}
}
@覆盖
公共无效的onMessage(字符串数据,IOAcknowledge ACK){
的System.out.println(服务器说:+数据);
}
@覆盖
公共无效onerror的(SocketIOException socketIOException){
的System.out.println(时出错);
socketIOException.printStackTrace();
}
@覆盖
公共无效onDisconnect(){
的System.out.println(连接终止。);
}
@覆盖
公共无效的onConnect(){
的System.out.println(连接建立);
}
@覆盖
在公共无效(String事件,IOAcknowledge ACK,对象参数... args){
的System.out.println(服务器触发的事件'+事件+');
}
});
//此行被缓存直到连接了现有的。
socket.send(你好服务器!);
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
和我的Node.js / Socket.io:
VAR应用=要求(HTTP)。createServer(句柄)
变种io的需要=('socket.io')(应用程序);
变种FS =要求(FS);
app.listen(1337);
功能处理器(REQ,RES){
fs.readFile(__目录名称+/index.html',
功能(ERR,数据){
如果(ERR){
res.writeHead(500);
返回res.end('错误加载的index.html');
}
res.writeHead(200);
res.end(数据);
});
}
io.on('连接',函数(插座){
socket.emit(新闻,{你好:'世界'});
socket.on(我的其它事件',功能(数据){
的console.log(数据);
});
});
和我得到了以下错误:
3月6日至9号:59:53.955:W / System.err的(11738):io.socket.SocketIOException:错误而握手
3月6日至9号:59:53.965:W / System.err的(11738):在io.socket.IOConnection.handshake(IOConnection.java:322)
3月6日至9号:59:53.965:W / System.err的(11738):在io.socket.IOConnection.access $ 7(IOConnection.java:292)
3月6日至9号:59:53.970:W / System.err的(11738):在io.socket.IOConnection $ ConnectThread.run(IOConnection.java:199)
3月6日至9号:59:53.970:W / System.err的(11738):java.io.FileNotFoundException:产生的原因http://192.168.1.60:1337/socket.io/1/
3月6日至9号:59:53.975:W / System.err的(11738):在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
3月6日至9号: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,消息:运输未知}
我不知道该怎么办,使其工作...请帮助:X
感谢:D
编辑:
由于 Larky 说,我不得不降级我的版本Socket.io来的 0.9.16 的。它的工作像以下节点code魅力:
VAR IO =需要('socket.io),听(80)。
io.on('连接',函数(插座){
socket.emit(新闻,{你好:'世界'});
socket.on(我的其它事件',功能(数据){
的console.log(数据);
});
});
解决方案
今天我恢复到socket.io在服务器端(在socket.io节点模块的早期版本)的早期版本解决了这个问题。或许协议改变了吗?
要做到这一点:
删除您node_modules文件夹中的文件夹socket.io
在控制台上键入:
NPM安装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)