简体   繁体   English

Java-使用TCP套接字接收通知

[英]Java - receiving notifications using TCP Sockets

Is there a good way to create receiving notification to Client using TCP Socket (without Pushes)? 有没有什么好方法可以创建使用TCP套接字(不带Push)的客户端接收通知?

It's accepted to send a request and receive an answer from server. 发送请求并从服务器接收答案已被接受。 But if I need to receive some messages from server in any time without sending any request (only in the beginning for connecting)? 但是,如果我需要在不发送任何请求的情况下随时从服务器接收一些消息(仅在连接开始时)? Like in messengers. 就像在信使中一样。

I think it's not ok to send a request to the server every 5 secs (for example) to get an answer. 我认为每隔5秒钟(例如)将请求发送到服务器以获得答案是不正确的。

The correct way to do that is to listen to websocket channels. 正确的方法是监听websocket频道。 I personally used WebSocket Rails-Android because our server is written on rails, but you can easily find a library that fits your need. 我个人使用WebSocket Rails-Android是因为我们的服务器是在Rails上编写的,但是您可以轻松找到适合您需要的库。

In the library above you can also find a simple and clear explanation of how the listening thing works. 在上面的库中,您还可以找到有关收听内容工作原理的简单明了的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM