我正在构建一个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
答案 0 :(得分:0)
我认为你需要显式配置套接字 - 这个代码来自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']);
});