Android:没有收到Socket.IO的任何回复

时间:2017-12-01 17:39:31

标签: android socket.io-1.0

我正在编写一个简单的代码来连接到Socket.Io服务器。

我只是在onCreate中连接到Socket.io并在按下按钮后推送一些消息。但不幸的是没有收到那里的单一回应。我的调试点没有进入 Emitter.Listener 。我正在执行以下代码:

public class MainChatPage extends Activity{

private Socket mSocket;
private EditText mInputMessageView;
private Button send;
private GlobalSharedPreference prefs;
private APIInterface apiInterface;
{
    try {
        mSocket = IO.socket(Constantstuffs.SERVER_URL);
    } catch (URISyntaxException e) {}
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_main_chat_page);
    apiInterface = APIClient.getClient().create(APIInterface.class);
    prefs = new GlobalSharedPreference(this);
    mInputMessageView = (EditText) findViewById(R.id.etMessege);
    send = (Button) findViewById(R.id.send_button);
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            attemptSend();
        }
    });


    mSocket.connect();
    mSocket.on("new message", onNewMessage);
}


private void attemptSend() {
    String message = mInputMessageView.getText().toString().trim();
    if (TextUtils.isEmpty(message)) {
        return;
    }

    mInputMessageView.setText("");
    mSocket.emit("new message", createJson(message));
}

private Emitter.Listener onNewMessage = new Emitter.Listener() {


    @Override
    public void call(final Object... args) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject data = (JSONObject) args[0];

                Log.v("some response", ""+data);
            }
        });
    }
};
    @Override
    public void onDestroy() {
        super.onDestroy();

        mSocket.disconnect();
        mSocket.off("new message", onNewMessage);
    }

0 个答案:

没有答案