Traffic Error Failure When Using Nodejs + SocketIO

I am creating an android chat application. I am using nodejs on the server and trying to implement the android client for socketIO using this . First, the client says hello to the server, and the server drives it back to the client. It works great. Now there is a Button that, when you click the text echo in the EditText to the server. It is assumed that the server will repeat the text back to the client. However, as soon as the text is sent to the server, I get a Discarding transport error on the server and nothing is returned. The client cannot repeat anything. 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; } } 

Server Error Log

 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 
+6
source share
1 answer

I think you need to explicitly configure the socket - this code is from the github documentation [https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO] - one of the polling options will make the problem go away.

 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']); }); 
0
source

Source: https://habr.com/ru/post/953588/


All Articles