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

Android与套接字连接时收到错误消息,第1张

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

在阅读了一些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 servervar 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);});// Routingapp.use(express.static(__dirname + '/public'));// Chatroom// usernames which are currently connected to the chatvar 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.access00(Manager.java:20)            at com.github.nkzawa.socketio.clIEnt.Manager.call(Manager.java:519)            at com.github.nkzawa.socketio.clIEnt.Manager.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.access0(Socket.java:29)            at com.github.nkzawa.engineio.clIEnt.socket.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.access0(PollingXHR.java:19)            at com.github.nkzawa.engineio.clIEnt.transports.PollingXHR.run(PollingXHR.java:126)            at com.github.nkzawa.thread.EventThread.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.如果您的服务器具有公共IP,则可能要使用它.

编辑

1个

换句话说,根据您的评论:您的PC IP为192.168.1.5.由于这是一个内部IP,因此您的androID必须连接到PC所在的同一子网,仅因为您能够发生connectin错误.基于此,我假设您需要在androID系统的地址栏中键入http://192.168.1.5/,以访问您的PC服务页面.假设,一个保持不变-脚本“ my androID code”正在您的androID上运行.因此,需要使用适当的主机(而不是localhost):192.168.1.5.不能告诉您您的androID是否阻塞了3000端口,但是从androID的角度来看,本地主机是不正确的,只要您不在该设备上运行nodeJs服务器.

同样,在移动设备上的浏览器缓存期间,该更改可能不会立即生效.

2

查看您的代码,我假设您也会对使用相同用户名的用户产生一些问题.是的,听起来很奇怪,但是用户可能希望在浏览器中打开几个连接到同一套接字服务器的选项卡.一旦那,您的用户名和numUsers变量将损坏.

只要应用是单实例专用的(例如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);}

您可能有所不同,完全基于聊天频道等.将套接字推到侦听器旁边,使我可以在同一节点脚本文件中运行单独的UDP服务器.目的是能够通过所有打开的选项卡监视/阻止/警告单个用户,如果它们分散在两个不同的浏览器中的话.

总结

以上是内存溢出为你收集整理的Android与套接字连接时收到错误消息全部内容,希望文章能够帮你解决Android与套接字连接时收到错误消息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存