This commit is contained in:
Redkale
2017-05-24 22:54:11 +08:00
parent 71c0763304
commit 4cd1b10c35
4 changed files with 176 additions and 0 deletions

View 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);
}
}

View 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;
}
}

View 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);
}
}

View 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);
}
}
}
}