This commit is contained in:
25
test/org/redkale/test/ws/ChatMessage.java
Normal file
25
test/org/redkale/test/ws/ChatMessage.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.test.ws;
|
||||||
|
|
||||||
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public class ChatMessage {
|
||||||
|
|
||||||
|
public int fromuserid;
|
||||||
|
|
||||||
|
public String fromusername;
|
||||||
|
|
||||||
|
public String content;
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return JsonConvert.root().convertTo(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
test/org/redkale/test/ws/ChatService.java
Normal file
24
test/org/redkale/test/ws/ChatService.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.test.ws;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import org.redkale.service.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public class ChatService implements Service {
|
||||||
|
|
||||||
|
private final Map<Integer, Integer> rooms = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public boolean joinRoom(int userid, int roomid) {
|
||||||
|
rooms.put(userid, roomid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
42
test/org/redkale/test/ws/ChatWebSocket.java
Normal file
42
test/org/redkale/test/ws/ChatWebSocket.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.test.ws;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.redkale.net.http.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
@RestWebSocket(name = "chat", catalog = "ws", comment = "文字聊天")
|
||||||
|
public class ChatWebSocket extends WebSocket<Integer, Object> {
|
||||||
|
|
||||||
|
protected static final AtomicInteger idcreator = new AtomicInteger(10000);
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
protected ChatService service;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CompletableFuture<Integer> createGroupid() {
|
||||||
|
return CompletableFuture.completedFuture(idcreator.incrementAndGet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@RestOnMessage(name = "sendmessage")
|
||||||
|
public void onChatMessage(ChatMessage message, Map<String, String> extmap) {
|
||||||
|
System.out.println("获取消息: message: " + message + ", map: " + extmap);
|
||||||
|
super.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RestOnMessage(name = "joinroom")
|
||||||
|
public void onJoinRoom(int roomid) {
|
||||||
|
service.joinRoom(getGroupid(), roomid);
|
||||||
|
System.out.println("加入房间: roomid: " + roomid);
|
||||||
|
}
|
||||||
|
}
|
||||||
85
test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java
Normal file
85
test/org/redkale/test/wsdync/_DyncChatWebSocketServlet.java
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.test.wsdync;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import org.redkale.net.http.*;
|
||||||
|
import org.redkale.test.ws.ChatMessage;
|
||||||
|
import org.redkale.test.ws.ChatService;
|
||||||
|
import org.redkale.test.ws.ChatWebSocket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
//@WebServlet("/ws/chat")
|
||||||
|
public final class _DyncChatWebSocketServlet extends WebSocketServlet {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatService service;
|
||||||
|
|
||||||
|
public _DyncChatWebSocketServlet() {
|
||||||
|
super();
|
||||||
|
this.messageTextType = _DyncChatWebSocketMessage.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected <G extends Serializable, T> WebSocket<G, T> createWebSocket() {
|
||||||
|
return (WebSocket) new _DyncChatWebSocket(service);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BiConsumer<WebSocket, Object> createRestOnMessageConsumer() {
|
||||||
|
return new RestOnMessageConsumer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class _DyncChatWebSocket extends ChatWebSocket {
|
||||||
|
|
||||||
|
public _DyncChatWebSocket(ChatService service) {
|
||||||
|
super();
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class _DyncChatWebSocketMessage {
|
||||||
|
|
||||||
|
public _DyncChatWebSocketMessage_sendmessagee sendmessage;
|
||||||
|
|
||||||
|
public _DyncChatWebSocketMessage_joinroom joinroom;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class _DyncChatWebSocketMessage_sendmessagee {
|
||||||
|
|
||||||
|
public ChatMessage message;
|
||||||
|
|
||||||
|
public Map<String, String> extmap;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class _DyncChatWebSocketMessage_joinroom {
|
||||||
|
|
||||||
|
public int roomid;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RestOnMessageConsumer implements BiConsumer<WebSocket, Object> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(WebSocket websocket0, Object message0) {
|
||||||
|
ChatWebSocket websocket = (ChatWebSocket) websocket0;
|
||||||
|
_DyncChatWebSocketMessage message = (_DyncChatWebSocketMessage) message0;
|
||||||
|
if (message.sendmessage != null) {
|
||||||
|
websocket.onChatMessage(message.sendmessage.message, message.sendmessage.extmap);
|
||||||
|
} else if (message.sendmessage != null) {
|
||||||
|
websocket.onJoinRoom(message.joinroom.roomid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user