java邀请实时通知的案例
时间: 2023-07-31 16:12:37 浏览: 48
下面是一个基于Java-WebSocket库实现的简单的邀请实时通知的案例:
服务端代码:
```java
import java.net.InetSocketAddress;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class InviteNotificationServer extends WebSocketServer {
public InviteNotificationServer(int port) {
super(new InetSocketAddress(port));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println("New connection from " + conn.getRemoteSocketAddress());
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println("Connection closed to " + conn.getRemoteSocketAddress()
+ " with exit code " + code + " because of " + reason);
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("Received message from " + conn.getRemoteSocketAddress() + ": " + message);
}
@Override
public void onError(WebSocket conn, Exception ex) {
System.out.println("Error occurred on connection to " + conn.getRemoteSocketAddress() + ": " + ex);
}
public static void main(String[] args) {
int port = 8080;
InviteNotificationServer server = new InviteNotificationServer(port);
server.start();
System.out.println("Invite notification server started on port " + server.getPort());
}
}
```
客户端代码:
```java
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Scanner;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
public class InviteNotificationClient extends WebSocketClient {
public InviteNotificationClient(URI serverUri) {
super(serverUri);
}
@Override
public void onOpen(ServerHandshake handshake) {
System.out.println("Connected to " + getURI());
}
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Connection closed with exit code " + code + " because of " + reason);
}
@Override
public void onError(Exception ex) {
System.out.println("Error occurred on connection to " + getURI() + ": " + ex);
}
public static void main(String[] args) throws URISyntaxException {
String serverUri = "ws://localhost:8080";
InviteNotificationClient client = new InviteNotificationClient(new URI(serverUri));
client.connect();
System.out.println("Connected to invite notification server at " + serverUri);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter invitation message (or 'exit' to quit): ");
String message = scanner.nextLine();
if (message.equals("exit")) {
client.close();
break;
}
client.send(message);
}
}
}
```
这个案例创建了一个WebSocket服务器,监听8080端口。当有新的WebSocket客户端连接时,服务器会打印出连接信息。当接收到客户端发送的消息时,服务器会打印出消息内容。
客户端连接到WebSocket服务器后,会不断等待用户输入邀请消息。当用户输入消息后,客户端会将消息发送到服务器。同时,客户端会不断接收服务器推送的消息,并打印出消息内容。
您可以根据您的需求修改此代码,实现更复杂的邀请实时通知功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)