kryonet客户端,无需打开新连接即可向服务器发送消息

时间:2017-06-30 16:30:09

标签: kryonet

我说我不是程序员,而是一个学习用Java编程一段时间的人。我希望在这里找到我的问题的解决方案。我正在尝试编程我的家庭自动化系统和遥控器,为此,我选择使用Kryonet。我的问题是每次我将数据发送到服务器时,客户端都会打开一个新连接。自从googlo开始已经有3个星期了,我试着弄明白该怎么做但没有结果。 每一个帮助都非常感谢。这是我的代码。谢谢。 此代码在我的家庭网络中工作。 对不起我的英文......

public class MainActivity extends AppCompatActivity {

Button button;
String IP = "";
EditText editText;
TextView textView;
EditText editText3;
public static String msg_response;
public static String msg_request;



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

    final Handler handler = new MyHandler();

    button = (Button) findViewById(R.id.button);
    editText = (EditText) findViewById(R.id.editText);
    editText3 = (EditText) findViewById(R.id.editText3);
    textView = (TextView) findViewById(R.id.textView);


    int MY_PERMISSIONS_REQUEST_INTERNET = 1;
    int MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.INTERNET},
            MY_PERMISSIONS_REQUEST_INTERNET);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
            MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE);
    int MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_WIFI_STATE},
            MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE);


    textView.setText(msg_response);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                msg_request = valueOf(editText3.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            MyThread myThread = new MyThread(handler);
            myThread.start();
        }
    });
}

 private class MyHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        if (bundle.containsKey("msg da server")) {
            String msgin = bundle.getString("msg da server");
            textView.setText(msgin);
        }
    }
}

class MyThread extends Thread {

    private Handler handler;

    public MyThread(Handler handler) {
        this.handler = handler;
    }


    public void run() {


        System.out.println("MyThread running");
        Client client = new Client();
        client.start();
        Kryo kryoClient = client.getKryo();
        kryoClient.register(SampleRequest.class);
        kryoClient.register(SampleResponse.class);

        try {
            client.connect(5000, "192.168.0.101", 54555, 54666);

        } catch (IOException e) {
            e.printStackTrace();

        }


        client.addListener(new Listener() {
            public void received(Connection connection, Object object) {
                if (object instanceof SampleResponse) {
                    SampleResponse response = (SampleResponse) object;
                    System.out.println(response.text);
                    msg_response = response.text.toString();
                    invia_activity(msg_response);
                }
            }
        });

        SampleRequest request = new SampleRequest();
        request.text = msg_request;

        client.sendTCP(request);
    }

    private void invia_activity(String invia) {
        Message msg = handler.obtainMessage();
        Bundle b = new Bundle();
        b.putString("msg da server", "" + invia);
        msg.setData(b);
        handler.sendMessage(msg);
    }
}

}

1 个答案:

答案 0 :(得分:0)

我没有直接的解决方案,但是我有一个教程。我用过同样的东西。因此,连接保持打开状态,您可以根据需要发送任意数量的数据包。它没有音频,但是代码运行良好。之后,您可以尝试使用该代码。这对我来说可以。 This is the tutorial 希望我能帮到你。 编辑: 也许您可以制作一个

public static Connection conn;

,您可以反复使用该对象作为与服务器的连接。