mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 18:26:23 +00:00
提交聊天
This commit is contained in:
@@ -4,4 +4,8 @@ public interface GActionEnum {
|
||||
|
||||
int TOKEN_EXPIRED = 1001; //Token过期
|
||||
|
||||
/*************** 聊天 *********************/
|
||||
int CHAT_MESSAGE = 2001; //发送聊天消息
|
||||
int CHAT_RECEIVE_MESSAGE = 2002; //接受聊天消息
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,21 @@
|
||||
package cn.jisol.game.actions;
|
||||
|
||||
import cn.jisol.game.network.client.GClient;
|
||||
import cn.jisol.game.proto.GUIMessage;
|
||||
import cn.jisol.ngame.actions.core.NAction;
|
||||
import cn.jisol.ngame.actions.core.NActionMethod;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@NAction
|
||||
public class GChatAction {
|
||||
|
||||
//发送消息
|
||||
@NActionMethod(GActionEnum.CHAT_MESSAGE)
|
||||
public static void onChatMessage(GUIMessage.GUIChatMessage message, Map<String, GClient> clients){
|
||||
clients.values().forEach(client -> {
|
||||
client.invoke(GActionEnum.CHAT_RECEIVE_MESSAGE,message);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@@ -1,11 +1,10 @@
|
||||
package cn.jisol.game.network;
|
||||
|
||||
import cn.jisol.game.actions.GActionEnum;
|
||||
import cn.jisol.game.data.Cache;
|
||||
import cn.jisol.game.controller.game.GPlayerController;
|
||||
import cn.jisol.game.network.client.GClient;
|
||||
import cn.jisol.ngame.actions.core.NActionEnum;
|
||||
import cn.jisol.ngame.client.NClient;
|
||||
import cn.jisol.ngame.network.JNetwork;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import javax.websocket.OnClose;
|
||||
@@ -24,20 +23,30 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Controller
|
||||
public class WebSocket {
|
||||
|
||||
public static final Map<String, NClient> CLIENTS = new ConcurrentHashMap<>();
|
||||
static GPlayerController playerController;
|
||||
|
||||
@Autowired
|
||||
private void setPlayerController(GPlayerController playerController){
|
||||
WebSocket.playerController = playerController;
|
||||
}
|
||||
|
||||
public static final Map<String, GClient> CLIENTS = new ConcurrentHashMap<>();
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session){
|
||||
|
||||
String token = session.getPathParameters().get("token");
|
||||
NClient client = new GClient(session);
|
||||
if(Objects.isNull(Cache.TOKEN.get(token))){
|
||||
GClient client = new GClient(token,session);
|
||||
|
||||
if(Objects.isNull(client.user)){
|
||||
//发送Token过期请求
|
||||
client.invoke(GActionEnum.TOKEN_EXPIRED);
|
||||
//关闭连接
|
||||
client.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
client.player = playerController.getPlayerInfo(client.user).data;
|
||||
CLIENTS.put(session.getId(),client);
|
||||
System.out.printf("[WebSocket] %s 连接成功.\n",session.getId());
|
||||
|
||||
@@ -45,7 +54,8 @@ public class WebSocket {
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(Session session, InputStream inputStream){
|
||||
JNetwork.onMessage(inputStream,CLIENTS.get(session.getId()),CLIENTS);
|
||||
GClient client = CLIENTS.get(session.getId());
|
||||
JNetwork.onMessage(inputStream,client,CLIENTS,client.user,client.player);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package cn.jisol.game.network.client;
|
||||
|
||||
import cn.jisol.game.data.Cache;
|
||||
import cn.jisol.game.entity.User;
|
||||
import cn.jisol.game.entity.game.Player;
|
||||
import cn.jisol.ngame.client.QueueNClient;
|
||||
|
||||
import javax.websocket.EncodeException;
|
||||
@@ -10,9 +13,14 @@ public class GClient extends QueueNClient {
|
||||
|
||||
public Session session;
|
||||
|
||||
public GClient(Session session){
|
||||
public User user;
|
||||
|
||||
public Player player;
|
||||
|
||||
public GClient(String token,Session session){
|
||||
super(session.getId());
|
||||
this.session = session;
|
||||
this.user = Cache.TOKEN.get(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,4 +40,5 @@ public class GClient extends QueueNClient {
|
||||
} catch (IOException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -23,41 +23,20 @@ public final class GUIMessage {
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return A list containing the message.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The message.
|
||||
*/
|
||||
java.util.List<java.lang.String>
|
||||
getMessageList();
|
||||
java.lang.String getMessage();
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return The count of message.
|
||||
*/
|
||||
int getMessageCount();
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the element to return.
|
||||
* @return The message at the given index.
|
||||
*/
|
||||
java.lang.String getMessage(int index);
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the value to return.
|
||||
* @return The bytes of the message at the given index.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The bytes for message.
|
||||
*/
|
||||
com.google.protobuf.ByteString
|
||||
getMessageBytes(int index);
|
||||
getMessageBytes();
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
@@ -76,7 +55,7 @@ public final class GUIMessage {
|
||||
super(builder);
|
||||
}
|
||||
private GUIChatMessage() {
|
||||
message_ = com.google.protobuf.LazyStringArrayList.EMPTY;
|
||||
message_ = "";
|
||||
}
|
||||
|
||||
@java.lang.Override
|
||||
@@ -99,7 +78,6 @@ public final class GUIMessage {
|
||||
if (extensionRegistry == null) {
|
||||
throw new java.lang.NullPointerException();
|
||||
}
|
||||
int mutable_bitField0_ = 0;
|
||||
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
|
||||
com.google.protobuf.UnknownFieldSet.newBuilder();
|
||||
try {
|
||||
@@ -112,11 +90,8 @@ public final class GUIMessage {
|
||||
break;
|
||||
case 10: {
|
||||
java.lang.String s = input.readStringRequireUtf8();
|
||||
if (!((mutable_bitField0_ & 0x00000001) != 0)) {
|
||||
message_ = new com.google.protobuf.LazyStringArrayList();
|
||||
mutable_bitField0_ |= 0x00000001;
|
||||
}
|
||||
message_.add(s);
|
||||
|
||||
message_ = s;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
@@ -136,9 +111,6 @@ public final class GUIMessage {
|
||||
throw new com.google.protobuf.InvalidProtocolBufferException(
|
||||
e).setUnfinishedMessage(this);
|
||||
} finally {
|
||||
if (((mutable_bitField0_ & 0x00000001) != 0)) {
|
||||
message_ = message_.getUnmodifiableView();
|
||||
}
|
||||
this.unknownFields = unknownFields.build();
|
||||
makeExtensionsImmutable();
|
||||
}
|
||||
@@ -157,54 +129,49 @@ public final class GUIMessage {
|
||||
}
|
||||
|
||||
public static final int MESSAGE_FIELD_NUMBER = 1;
|
||||
private com.google.protobuf.LazyStringList message_;
|
||||
private volatile java.lang.Object message_;
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return A list containing the message.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The message.
|
||||
*/
|
||||
public com.google.protobuf.ProtocolStringList
|
||||
getMessageList() {
|
||||
return message_;
|
||||
@java.lang.Override
|
||||
public java.lang.String getMessage() {
|
||||
java.lang.Object ref = message_;
|
||||
if (ref instanceof java.lang.String) {
|
||||
return (java.lang.String) ref;
|
||||
} else {
|
||||
com.google.protobuf.ByteString bs =
|
||||
(com.google.protobuf.ByteString) ref;
|
||||
java.lang.String s = bs.toStringUtf8();
|
||||
message_ = s;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return The count of message.
|
||||
*/
|
||||
public int getMessageCount() {
|
||||
return message_.size();
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the element to return.
|
||||
* @return The message at the given index.
|
||||
*/
|
||||
public java.lang.String getMessage(int index) {
|
||||
return message_.get(index);
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the value to return.
|
||||
* @return The bytes of the message at the given index.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The bytes for message.
|
||||
*/
|
||||
@java.lang.Override
|
||||
public com.google.protobuf.ByteString
|
||||
getMessageBytes(int index) {
|
||||
return message_.getByteString(index);
|
||||
getMessageBytes() {
|
||||
java.lang.Object ref = message_;
|
||||
if (ref instanceof java.lang.String) {
|
||||
com.google.protobuf.ByteString b =
|
||||
com.google.protobuf.ByteString.copyFromUtf8(
|
||||
(java.lang.String) ref);
|
||||
message_ = b;
|
||||
return b;
|
||||
} else {
|
||||
return (com.google.protobuf.ByteString) ref;
|
||||
}
|
||||
}
|
||||
|
||||
private byte memoizedIsInitialized = -1;
|
||||
@@ -221,8 +188,8 @@ public final class GUIMessage {
|
||||
@java.lang.Override
|
||||
public void writeTo(com.google.protobuf.CodedOutputStream output)
|
||||
throws java.io.IOException {
|
||||
for (int i = 0; i < message_.size(); i++) {
|
||||
com.google.protobuf.GeneratedMessageV3.writeString(output, 1, message_.getRaw(i));
|
||||
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) {
|
||||
com.google.protobuf.GeneratedMessageV3.writeString(output, 1, message_);
|
||||
}
|
||||
unknownFields.writeTo(output);
|
||||
}
|
||||
@@ -233,13 +200,8 @@ public final class GUIMessage {
|
||||
if (size != -1) return size;
|
||||
|
||||
size = 0;
|
||||
{
|
||||
int dataSize = 0;
|
||||
for (int i = 0; i < message_.size(); i++) {
|
||||
dataSize += computeStringSizeNoTag(message_.getRaw(i));
|
||||
}
|
||||
size += dataSize;
|
||||
size += 1 * getMessageList().size();
|
||||
if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) {
|
||||
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, message_);
|
||||
}
|
||||
size += unknownFields.getSerializedSize();
|
||||
memoizedSize = size;
|
||||
@@ -256,8 +218,8 @@ public final class GUIMessage {
|
||||
}
|
||||
GUIMessage.GUIChatMessage other = (GUIMessage.GUIChatMessage) obj;
|
||||
|
||||
if (!getMessageList()
|
||||
.equals(other.getMessageList())) return false;
|
||||
if (!getMessage()
|
||||
.equals(other.getMessage())) return false;
|
||||
if (!unknownFields.equals(other.unknownFields)) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -269,10 +231,8 @@ public final class GUIMessage {
|
||||
}
|
||||
int hash = 41;
|
||||
hash = (19 * hash) + getDescriptor().hashCode();
|
||||
if (getMessageCount() > 0) {
|
||||
hash = (37 * hash) + MESSAGE_FIELD_NUMBER;
|
||||
hash = (53 * hash) + getMessageList().hashCode();
|
||||
}
|
||||
hash = (37 * hash) + MESSAGE_FIELD_NUMBER;
|
||||
hash = (53 * hash) + getMessage().hashCode();
|
||||
hash = (29 * hash) + unknownFields.hashCode();
|
||||
memoizedHashCode = hash;
|
||||
return hash;
|
||||
@@ -410,8 +370,8 @@ public final class GUIMessage {
|
||||
@java.lang.Override
|
||||
public Builder clear() {
|
||||
super.clear();
|
||||
message_ = com.google.protobuf.LazyStringArrayList.EMPTY;
|
||||
bitField0_ = (bitField0_ & ~0x00000001);
|
||||
message_ = "";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -438,11 +398,6 @@ public final class GUIMessage {
|
||||
@java.lang.Override
|
||||
public GUIMessage.GUIChatMessage buildPartial() {
|
||||
GUIMessage.GUIChatMessage result = new GUIMessage.GUIChatMessage(this);
|
||||
int from_bitField0_ = bitField0_;
|
||||
if (((bitField0_ & 0x00000001) != 0)) {
|
||||
message_ = message_.getUnmodifiableView();
|
||||
bitField0_ = (bitField0_ & ~0x00000001);
|
||||
}
|
||||
result.message_ = message_;
|
||||
onBuilt();
|
||||
return result;
|
||||
@@ -492,14 +447,8 @@ public final class GUIMessage {
|
||||
|
||||
public Builder mergeFrom(GUIMessage.GUIChatMessage other) {
|
||||
if (other == GUIMessage.GUIChatMessage.getDefaultInstance()) return this;
|
||||
if (!other.message_.isEmpty()) {
|
||||
if (message_.isEmpty()) {
|
||||
message_ = other.message_;
|
||||
bitField0_ = (bitField0_ & ~0x00000001);
|
||||
} else {
|
||||
ensureMessageIsMutable();
|
||||
message_.addAll(other.message_);
|
||||
}
|
||||
if (!other.getMessage().isEmpty()) {
|
||||
message_ = other.message_;
|
||||
onChanged();
|
||||
}
|
||||
this.mergeUnknownFields(other.unknownFields);
|
||||
@@ -530,99 +479,65 @@ public final class GUIMessage {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
private int bitField0_;
|
||||
|
||||
private com.google.protobuf.LazyStringList message_ = com.google.protobuf.LazyStringArrayList.EMPTY;
|
||||
private void ensureMessageIsMutable() {
|
||||
if (!((bitField0_ & 0x00000001) != 0)) {
|
||||
message_ = new com.google.protobuf.LazyStringArrayList(message_);
|
||||
bitField0_ |= 0x00000001;
|
||||
}
|
||||
}
|
||||
private java.lang.Object message_ = "";
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return A list containing the message.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The message.
|
||||
*/
|
||||
public com.google.protobuf.ProtocolStringList
|
||||
getMessageList() {
|
||||
return message_.getUnmodifiableView();
|
||||
public java.lang.String getMessage() {
|
||||
java.lang.Object ref = message_;
|
||||
if (!(ref instanceof java.lang.String)) {
|
||||
com.google.protobuf.ByteString bs =
|
||||
(com.google.protobuf.ByteString) ref;
|
||||
java.lang.String s = bs.toStringUtf8();
|
||||
message_ = s;
|
||||
return s;
|
||||
} else {
|
||||
return (java.lang.String) ref;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @return The count of message.
|
||||
*/
|
||||
public int getMessageCount() {
|
||||
return message_.size();
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the element to return.
|
||||
* @return The message at the given index.
|
||||
*/
|
||||
public java.lang.String getMessage(int index) {
|
||||
return message_.get(index);
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index of the value to return.
|
||||
* @return The bytes of the message at the given index.
|
||||
* <code>string message = 1;</code>
|
||||
* @return The bytes for message.
|
||||
*/
|
||||
public com.google.protobuf.ByteString
|
||||
getMessageBytes(int index) {
|
||||
return message_.getByteString(index);
|
||||
getMessageBytes() {
|
||||
java.lang.Object ref = message_;
|
||||
if (ref instanceof String) {
|
||||
com.google.protobuf.ByteString b =
|
||||
com.google.protobuf.ByteString.copyFromUtf8(
|
||||
(java.lang.String) ref);
|
||||
message_ = b;
|
||||
return b;
|
||||
} else {
|
||||
return (com.google.protobuf.ByteString) ref;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param index The index to set the value at.
|
||||
* <code>string message = 1;</code>
|
||||
* @param value The message to set.
|
||||
* @return This builder for chaining.
|
||||
*/
|
||||
public Builder setMessage(
|
||||
int index, java.lang.String value) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
ensureMessageIsMutable();
|
||||
message_.set(index, value);
|
||||
onChanged();
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param value The message to add.
|
||||
* @return This builder for chaining.
|
||||
*/
|
||||
public Builder addMessage(
|
||||
java.lang.String value) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
ensureMessageIsMutable();
|
||||
message_.add(value);
|
||||
|
||||
message_ = value;
|
||||
onChanged();
|
||||
return this;
|
||||
}
|
||||
@@ -631,29 +546,12 @@ public final class GUIMessage {
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param values The message to add.
|
||||
* @return This builder for chaining.
|
||||
*/
|
||||
public Builder addAllMessage(
|
||||
java.lang.Iterable<java.lang.String> values) {
|
||||
ensureMessageIsMutable();
|
||||
com.google.protobuf.AbstractMessageLite.Builder.addAll(
|
||||
values, message_);
|
||||
onChanged();
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* <pre>
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* <code>string message = 1;</code>
|
||||
* @return This builder for chaining.
|
||||
*/
|
||||
public Builder clearMessage() {
|
||||
message_ = com.google.protobuf.LazyStringArrayList.EMPTY;
|
||||
bitField0_ = (bitField0_ & ~0x00000001);
|
||||
|
||||
message_ = getDefaultInstance().getMessage();
|
||||
onChanged();
|
||||
return this;
|
||||
}
|
||||
@@ -662,18 +560,18 @@ public final class GUIMessage {
|
||||
*聊天内容
|
||||
* </pre>
|
||||
*
|
||||
* <code>repeated string message = 1;</code>
|
||||
* @param value The bytes of the message to add.
|
||||
* <code>string message = 1;</code>
|
||||
* @param value The bytes for message to set.
|
||||
* @return This builder for chaining.
|
||||
*/
|
||||
public Builder addMessageBytes(
|
||||
public Builder setMessageBytes(
|
||||
com.google.protobuf.ByteString value) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
checkByteStringIsUtf8(value);
|
||||
ensureMessageIsMutable();
|
||||
message_.add(value);
|
||||
|
||||
message_ = value;
|
||||
onChanged();
|
||||
return this;
|
||||
}
|
||||
@@ -745,7 +643,7 @@ public final class GUIMessage {
|
||||
static {
|
||||
java.lang.String[] descriptorData = {
|
||||
"\n\020GUIMessage.proto\"!\n\016GUIChatMessage\022\017\n\007" +
|
||||
"message\030\001 \003(\tB\026\n\024cn.jisol.ngame.protob\006p" +
|
||||
"message\030\001 \001(\tB\026\n\024cn.jisol.ngame.protob\006p" +
|
||||
"roto3"
|
||||
};
|
||||
descriptor = com.google.protobuf.Descriptors.FileDescriptor
|
||||
|
@@ -4,5 +4,5 @@ option java_package = "cn.jisol.ngame.proto";
|
||||
|
||||
//聊天信息
|
||||
message GUIChatMessage {
|
||||
repeated string message = 1; //聊天内容
|
||||
string message = 1; //聊天内容
|
||||
}
|
||||
|
Reference in New Issue
Block a user