android - 使用 Nodejs + SocketIO 时丢弃传输错误

标签 android node.js socket.io

我正在构建一个 android 聊天应用程序。我在服务器端使用 nodejs 并尝试使用 this 为 socketIO 实现 android 客户端.首先,客户端向服务器回显“hello”,然后服务器将其回显给客户端。这很好用。现在有一个 Button,按下时会将 EditText 中的文本回显到服务器。服务器应该将文本回显给客户端。但是,一旦文本回显到服务器,我就会在服务器端收到 Discarding transport 错误,并且没有任何回显。客户端无法进一步回显任何内容。代码有什么问题?

服务器

var http = require('http'),fs = require('fs');

var app = http.createServer(function (req, res) {

res.end();
                                                      }).listen(8000, '127.0.0.1');

var io = require('socket.io').listen(app);

io.sockets.on('connection', function(socket) {
socket.on('echo', function(data) {
socket.emit('echoback', data);
});
});

客户端

 package com.jack.pri;
 import java.net.MalformedURLException;
 import org.json.JSONException;
 import org.json.JSONObject;
 import android.os.Bundle;
 import android.app.Activity;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnKeyListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;
 import io.socket.*;

    public class MainActivity extends Activity {
private SocketIO socket;
private TextView tview;
private Button btn;
private EditText tt;
private String k;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tview=(TextView) findViewById(R.id.tv);

     tt = (EditText) findViewById(R.id.et);
          btn = (Button) findViewById(R.id.button1);


    //socket = null;
    try {
        socket = new SocketIO("http://10.0.2.2:8000");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    socket.connect(new IOCallback() {
        @Override
        public void on(String event, IOAcknowledge ack, Object... args) {
            if ("echoback".equals(event) && args.length > 0) {
               tview.setText(""+args[0]);
               Log.e("received",""+args[0]);
            }
        }

        @Override
        public void onMessage(JSONObject json, IOAcknowledge ack) {}
        @Override
        public void onMessage(String data, IOAcknowledge ack) {}
        @Override
        public void onError(SocketIOException socketIOException) { socketIOException.printStackTrace();}
        @Override
        public void onDisconnect() {}
        @Override
        public void onConnect() {}
    });

    ///
    socket.emit("echo", "hello");

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

   k=tt.getText().toString();
   socket.emit("echo", k);          

        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

服务器端的错误日志

debug - client authorized
info  - handshake authorized kaHbMG-lFmuFtvkFGY2W
debug - setting request GET /socket.io/1/websocket/kaHbMG-lFmuFtvkFGY2W
debug - set heartbeat interval for client kaHbMG-lFmuFtvkFGY2W
debug - client authorized for 
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"echoback","args":["hello"]}
debug - emitting heartbeat for client kaHbMG-lFmuFtvkFGY2W
debug - websocket writing 2::
debug - set heartbeat timeout for client kaHbMG-lFmuFtvkFGY2W
debug - got heartbeat packet
debug - cleared heartbeat timeout for client kaHbMG-lFmuFtvkFGY2W
debug - set heartbeat interval for client kaHbMG-lFmuFtvkFGY2W
debug - websocket writing 5:::{"name":"echoback","args":["this is textbox input text"]}
info  - transport end (undefined)
debug - set close timeout for client kaHbMG-lFmuFtvkFGY2W
debug - cleared close timeout for client kaHbMG-lFmuFtvkFGY2W
debug - cleared heartbeat interval for client kaHbMG-lFmuFtvkFGY2W
debug - discarding transport

最佳答案

我认为您需要显式配置套接字 - 这段代码来自 github 文档 [https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO] - 轮询选项之一将使问题消失了。

var io = require('socket.io').listen(80);

io.configure('production', function(){
  io.enable('browser client etag');
  io.set('log level', 1);

  io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);
});

io.configure('development', function(){
  io.set('transports', ['websocket']);
});

关于android - 使用 Nodejs + SocketIO 时丢弃传输错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727625/

相关文章:

android - 堆检测失败,退出状态为 1 [Android]

java - WP10检测到的蓝牙设备功能

Android:将文件保存到可以稍后查看的下载

node.js - 不使用框架的纯 Node.js 文件上传(多部分 POST)

node.js - socket.io 与 Rest api 之间的资源比较

android - 单击并长按按钮时在线性布局中添加阴影

node.js - 环回 - 连接器 Hook 中的 "Cannot read property ' 连接器为 null”

mysql - 如何在 nodejs/socket.io 中的客户端请求后对 mysql 数据库进行查询?

javascript - Node js redis socket.io pubsub实时更新

javascript - 通过 socket.io 添加 ssl 后无法发出消息