gpt4 book ai didi

Android 在连接到 Socket 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:32 28 4
gpt4 key购买 nike

在阅读一些 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
});
}
});
});

我的安卓代码:

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)

最佳答案

我会责怪这个:

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

我假设您没有在您的 android 上运行您的 node.js 服务器,但可能在您的 PC 上。如果是这样,在您的 android 上进行测试时,您正在尝试通过端口 3000 连接回您的 android 本身 - 因为 localhost 链接到设备本身。

如果您在服务器和 android 上使用相同的本地网络,您应该检查您的 PC 的 IP 并将其代替 localhost。如果您的服务器有公共(public) IP,您可能想改用它。


编辑

1

换句话说,根据您的评论:您的 PC IP 是 192.168.1.5。由于这是一个内部 IP,您的 android 必须连接到您的 PC 所在的同一子网,因为您可能会发生连接错误。基于此,我假设您需要在 android 的地址栏中键入 http://192.168.1.5/,才能访问您的 PC 正在服务的页面。假设,一个保持不变 - 脚本“我的 android 代码”正在您的 android 上运行。因此,需要一个合适的主机来代替 localhost:192.168.1.5。无法判断您的 android 是否阻塞了 3000 端口,但是从 android 的角度来看 localhost 是不正确的,只要您没有在该设备上运行您的 nodejs 服务器。

此外,在移动设备上的浏览器缓存期间,该更改可能不会临时生效。

2

查看您的代码,我假设您也会遇到一些用户使用相同用户名的问题。是的,听起来很奇怪,但用户可能想在浏览器中打开几个选项卡,连接到同一个套接字服务器。一旦发生这种情况,您的 usernamesnumUsers 变量就会损坏。

只要应用是单实例专用的(例如 player@game),我就会使用

usernames[username] = socket

将套接字放在一边,能够发布跨玩家相关事件,避免迭代所有打开的套接字。

同样出于聊天目的,您可能希望允许用户同时在几个浏览器选项卡上进行连接。通常我以这种方式存储所有套接字:

if (!users[user]) {
users[user] = {
sockets: [socket]
};
console.log(sprintf('[%s] [CONNECTED] User %s', Date(), user));

} else {
users[user].sockets.push(socket);
}

您可能会有所不同,主要基于聊天 channel 等。将套接字放在监听器旁边允许我在同一 Node 脚本文件中运行单独的 UDP 服务器。它的目的是能够通过所有打开的选项卡监视/阻止/提醒单个用户,如果它们分布在两个不同的浏览器上。

关于Android 在连接到 Socket 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28763858/

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