basic4android tcp,Android与套接字连接时收到错误消息

在阅读了一些express.io文档并成功连接到http://chat.socket.io之后,我尝试使用nodejs和express.io编写简单的应用程序,我发现了使用nodejs和express.io创建服务器端的简单示例,在命令行中运行以下代码并在浏览器中打开http:// localhost:3000之后,我没有收到任何错误,我找不到关于http://chat.socket.io服务器中编码的任何好的文档,现在我想要尝试将示例从android客户端发送到服务器的请求,但出现连接错误:

错误:

CONNECTION ERROR

server.js:

// Setup basic express server

var express = require('express');

var app = express();

var server = require('http').createServer(app);

var io = require('../..')(server);

var port = process.env.PORT || 3000;

server.listen(port, function () {

console.log('Server listening at port %d', port);

});

// Routing

app.use(express.static(__dirname + '/public'));

// Chatroom

// usernames which are currently connected to the chat

var usernames = {};

var numUsers = 0;

io.on('connection', function (socket) {

var addedUser = false;

// when the client emits 'new message', this listens and executes

socket.on('new message', function (data) {

// we tell the client to execute 'new message'

socket.broadcast.emit('new message', {

username: socket.username,

message: data

});

});

// when the client emits 'add user', this listens and executes

socket.on('add user', function (username) {

// we store the username in the socket session for this client

socket.username = username;

// add the client's username to the global list

usernames[username] = username;

++numUsers;

addedUser = true;

socket.emit('login', {

numUsers: numUsers

});

// echo globally (all clients) that a person has connected

socket.broadcast.emit('user joined', {

username: socket.username,

numUsers: numUsers

});

});

// when the client emits 'typing', we broadcast it to others

socket.on('typing', function () {

socket.broadcast.emit('typing', {

username: socket.username

});

});

// when the client emits 'stop typing', we broadcast it to others

socket.on('stop typing', function () {

socket.broadcast.emit('stop typing', {

username: socket.username

});

});

// when the user disconnects.. perform this

socket.on('disconnect', function () {

// remove the username from global usernames list

if (addedUser) {

delete usernames[socket.username];

--numUsers;

// echo globally that this client has left

socket.broadcast.emit('user left', {

username: socket.username,

numUsers: numUsers

});

}

});

});

我的Android代码:

private Socket mSocket;

{

try {

/* connection successful to http://chat.socket.io */

mSocket = IO.socket("http://localhost:3000");

} catch (URISyntaxException e) {

Log.e("Error URI", String.valueOf(e));

throw new RuntimeException(e);

}

}

public void onCreate(Bundle savedInstanceState) {

...

mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);

mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);

mSocket.on("new message", onNewMessage);

mSocket.on("user joined", onUserJoined);

mSocket.on("user left", onUserLeft);

mSocket.on("typing", onTyping);

mSocket.on("stop typing", onStopTyping);

mSocket.connect();

...

Button signInButton = (Button) findViewById(R.id.sign_in_button);

signInButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

attemptLogin();

}

});

mSocket.on("login", onLogin);

}

private void attemptLogin() {

mUsernameView.setError(null);

String username = mUsernameView.getText().toString().trim();

if (TextUtils.isEmpty(username)) {

mUsernameView.setError(getString(R.string.error_field_required));

mUsernameView.requestFocus();

return;

}

mUsername = username;

mSocket.emit("add user", username);

}

Android错误:

E/AndroidRuntime﹕ FATAL EXCEPTION: EventThread

java.lang.IllegalArgumentException: delay < 0: -432345566375051264

at java.util.Timer.schedule(Timer.java:457)

at com.github.nkzawa.socketio.client.Manager.reconnect(Manager.java:497)

at com.github.nkzawa.socketio.client.Manager.access$2000(Manager.java:20)

at com.github.nkzawa.socketio.client.Manager$8$1$1.call(Manager.java:519)

at com.github.nkzawa.socketio.client.Manager$1$3.call(Manager.java:282)

at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)

at com.github.nkzawa.engineio.client.Socket.onError(Socket.java:754)

at com.github.nkzawa.engineio.client.Socket.access$800(Socket.java:29)

at com.github.nkzawa.engineio.client.Socket$4.call(Socket.java:293)

at com.github.nkzawa.emitter.Emitter.emit(Emitter.java:117)

at com.github.nkzawa.engineio.client.Transport.onError(Transport.java:63)

at com.github.nkzawa.engineio.client.transports.PollingXHR.access$100(PollingXHR.java:19)

at com.github.nkzawa.engineio.client.transports.PollingXHR$6$1.run(PollingXHR.java:126)

at com.github.nkzawa.thread.EventThread$2.run(EventThread.java:75)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

at java.lang.Thread.run(Thread.java:838)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值