简体   繁体   English

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

[英]Discarding transport error while using Nodejs + SocketIO

I am building an android chat application. 我正在构建一个android聊天应用程序。 I am using nodejs at server end and trying to implement android client for socketIO using this . 我在服务器端使用nodejs并尝试使用实现socketIO的android客户端。 First the client echoes "hello" to the server and the server echoes it back to the client. 首先,客户端向服务器回送“hello”,然后服务器将其回送给客户端。 This works fine. 这很好用。 Now there is a Button , which when pressed echoes the text in the EditText to the server. 现在有一个Button ,按下时EditText的文本回显到服务器。 Server is supposed to echo the text back to the client. 服务器应该将文本回送给客户端。 However, as soon as text is echoed to the server, I get Discarding transport error at server end and nothing is echoed back. 但是,只要文本回显到服务器,我就会在服务器端获得Discarding transport错误,并且不会回显任何内容。 Client is unable to echo anything further. 客户无法进一步回应任何事情。 What is wrong with the codes ? 这些代码有什么问题?

Server 服务器

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);
});
});

Client 客户

 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;
}

}

Errorlog at server end 服务器端的错误日志

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

I think you need to explicitly configure the socket - this code from the github documentation [https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO] - one of the polling options would make the problem go away. 我认为你需要显式配置套接字 - 这个代码来自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']);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM