Reputation: 29
I'm trying to develop an android application which will connect to the node.js server. Here's error msg:
04-28 14:24:52.250: W/System.err(811): io.socket.SocketIOException: Error while handshaking
04-28 14:24:52.250: W/System.err(811): at io.socket.IOConnection.handshake(IOConnection.java:322)
04-28 14:24:52.259: W/System.err(811): at io.socket.IOConnection.access$600(IOConnection.java:39)
04-28 14:24:52.259: W/System.err(811): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
04-28 14:24:52.259: W/System.err(811): Caused by: java.lang.ArrayIndexOutOfBoundsException
04-28 14:24:52.306: W/System.err(811): at io.socket.IOConnection.handshake(IOConnection.java:318)
04-28 14:24:52.306: W/System.err(811): ... 2 more
Server:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8881, '192.168.1.107');
console.log('Server running at http://192.168.1.107:8881/');
Client:
public class RestClientActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
SocketIO socket = new SocketIO("http://192.168.1.107:8881/");
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();
}
}
Any ideas what I'm doing wrong? If I type http://192.168.1.107:8881/
in browser it will display me "Hello World" on the page but I can't connect using socket.io-java-client library.
Upvotes: 2
Views: 7164
Reputation: 7155
First IF you try to work a rest client for your service using socket.io
IS NOT necessary just stick with HTTP server and you will be fine, on client-side use HTTPClient ..
Assuming you use this android socket.io client Java Socket.io client which i have also worked with.
Try this as server-side socket io script which is the basic example at LearnBoost/socket.io
var server = require('http').Server();
var io = require('socket.io')(server);
io.on('connection', function(socket){
socket.on('message', function(data){
console.log("Received message : "+data);
socket.emit("message","echoing back : "+data);
});
socket.on('disconnect', function(){
// client disconnected
});
});
server.listen(8881,"192.168.1.107");
Upvotes: 2