应用未监听套接字事件。问题出在哪里?

时间:2019-04-06 19:17:18

标签: android node.js socket.io

“我似乎没有任何错误,但是我无法获取日志消息。服务器中的控制台消息也可以正常工作,但是只有应用程序未在监听“ userjoinedthechat”事件。请帮助? 这是我的MainActivity。”

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.github.nkzawa.socketio.client.Socket;
public class MainActivity extends AppCompatActivity {
private Socket socket;
private Button button;
private EditText edt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ChatAplication aplication = (ChatAplication) getApplication();
    socket = aplication.getSocket();

    button = findViewById(R.id.button);
    edt = findViewById(R.id.editText);
    //Onclick Button
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = edt.getText().toString().trim();
            socket.emit("join",name);
                Intent intent = new 
  Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
        }
    });
}}

“这是我的SecondActivity”

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Socket;

import org.json.JSONException;
import org.json.JSONObject;

public class SecondActivity extends AppCompatActivity {

private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    ChatAplication chatAplication = (ChatAplication) getApplication();
    socket = chatAplication.getSocket();
    socket.on("userjoinedthechat", new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               JSONObject object = (JSONObject) args[0];

               try {
                //   JSONObject data = object.getJSONObject("users");
                   String str = object.getString("name");
                   String str2 = object.getString("id");
                   Log.d("errors",object.toString());
                   Log.d("errors",str);
                   Log.d("errors",str2);
               } catch (JSONException e) {
                   e.printStackTrace();
                   Log.d("errors","listen failed");
               }

           }
       });
        }
    });
   }}

“这是聊天应用程序”

import android.app.Application;

import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

 import java.net.URISyntaxException;


public class ChatAplication extends Application {
private Socket socket;

public Socket getSocket() {
    return socket;
}

private static final String URL = "http://192.168.43.191:3000";

@Override
public void onCreate() {
    super.onCreate();
    try {
        socket = IO.socket(URL);
        socket.connect();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
 }}

“最后这是我的服务器端代码:index.js”

 const express = require('express'),
 http = require('http'),
 app = express(),
 server = http.createServer(app),
 io = require('socket.io').listen(server);
 app.get('/', (req, res) => {


 res.send('Chat Server is running on port 3000')
 });

 var users = {"2Xyzaques": "qwefvmve12"}; //Map design

 io.on('connection', (socket) => {

 console.log('user connected');

 socket.on('join', function(userNickname) {
  var u = userNickname;

    console.log(u +" : has joined the chat "  );

    socket.emit('listusers', {
    users: users
   });

     users[socket.id] = userNickname;
 //Broadcast the id of the joined   
 let details = {"name": userNickname, "id": socket.id} 

    socket.broadcast.emit('userjoinedthechat',details);
 })

 socket.on('messagedetection', (socket,data) => {

 socket.to(data.id).emit('newmessage', {
    message: data.message,
    name: users[socket.id]
 });
  })

 socket.on('disconnect', function() {

     delete users[socket.id];
    console.log(' has left ')

    socket.broadcast.emit( "userdisconnect" ,{
      id: socket.id
    });
 })
 })

 server.listen(3000,()=>{

 console.log('Node app is running on port 3000')

 })

0 个答案:

没有答案