/// <summary> /// 连接到服务器 /// </summary> /// <param name="hostname"></param> /// <param name="port"></param> /// <returns></returns> public void Connect(string hostname, ushort port) { _lastServerHostname = hostname; _lastServerPort = port; if (State == MinecraftClientState.InGame) { _adapter.Disconnect(); } _logger.Info($"Connecting {hostname}, {port}"); _adapter = new MinecraftClientAdapter(hostname, port, this); _adapter.Disconnected += (sender, e) => { State = MinecraftClientState.InTitle; Disconnected?.Invoke(sender, e); _world = null; _player = null; }; _adapter.Chat += (sender, e) => { //TODO: Use ChatEventArgs and Minecraft.Text.RichText ChatReceived?.Invoke(sender, e.jsonData); }; _player = new ClientPlayerEntityHandler(_adapter); _world = new WorldHandler(_adapter, _player); _adapter.Connect(); State = MinecraftClientState.InGame; }
public CustomMessages() { CustomMessagingManager.RegisterNamedMessageHandler(MessageName, (sender, payload) => { Debug.Log("MESSAGE HANDLER"); using (var reader = PooledBitReader.Get(payload)) { ChatReceived?.Invoke(this, reader.ReadString().ToString()); } }); }
private void MessageReceived(object sender, IMessageBase message) { try { switch (message.MessageType) { case MessageType.ChatMessage: ChatReceived?.Invoke(this, message as ChatMessage); break; case MessageType.DataStateChanged: DataStateChanged?.Invoke(this, EventArgs.Empty); break; case MessageType.LocalEvent: LocalEventReceived?.Invoke(this, message as LocalEventMessage); break; case MessageType.LocationChanged: LocationChanged?.Invoke(this, EventArgs.Empty); break; case MessageType.PartyInviteReceived: var invite = message as PartyInvite; _toastService.ShowToast($"Party invite from {invite.From.Name}."); PartyInviteReceived?.Invoke(this, invite); break; case MessageType.ToastMessage: if (message is ToastMessage toastMessage) { _toastService.ShowToast(toastMessage.Message, toastMessage.ExpirationMs, toastMessage.ClassString, toastMessage.StyleOverride); } break; default: break; } } catch (Exception ex) { _logger.LogError(ex, "Error while processing message."); } }
private void Wc_Received(object sender, string e) { //{"ping":{"content":"rs:0"}} //{"ping":{"content":"ps:0"}} //{"thread":{"resultcode":0,"thread":1651546717,"last_res":179,"ticket":"0x5ad4480","revision":1,"server_time":1559313215}} //{"chat":{"thread":1651546717,"no":1,"vpos":12833,"date":1559311996,"date_usec":168105,"user_id":"89121734","content":"待ってた"}} //{"ping":{"content":"pf:0"}} //{"ping":{"content":"rf:0"}} //{"chat":{"thread":1651546717,"no":658,"vpos":527866,"date":1559317149,"date_usec":731458,"mail":"184","user_id":"c1_CPPYyCY1VBN2nBvbUSSfwF3g","anonymity":1,"content":"このきちげぇにチェーンソーで切られるぞ"}} var raw = e; Debug.WriteLine(raw); var d = DynamicJson.Parse(raw); if (d.IsDefined("chat")) { var chat = ParseChat(raw); ChatReceived?.Invoke(this, new ReceivedChat { IsInitialComment = _isInitialComment, Message = chat }); } else if (d.IsDefined("ping")) { var content = (string)d.ping.content; if (content == "rf:0") { _isInitialComment = false; } } else if (d.IsDefined("thread")) { var thread = ParseThread(raw); ThreadReceived?.Invoke(this, thread); } else { throw new ParseException(raw); } }
protected virtual void OnChatReceived(ChatEventArgs e) { ChatReceived?.Invoke(this, e); }
//接收信息 private void ActiveMQ_Received(object sender, string e) { var package = JsonConvert.DeserializeObject <Package>(e); switch (package.Type) { case "Response": { switch (package.Method) { case nameof(RegisterUser): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(int) }); RegisterUserResponse?.Invoke(this, data.Result); } } break; case nameof(UserLogin): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, List <User> >) }); UserLoginResponse?.Invoke(this, data.Result); } } break; case nameof(SearchFriends): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, List <User> >) }); FriendsSearchedResponse?.Invoke(this, data.Result.Item2); } } break; case nameof(AddFriend): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, User, User>) }); AddFriendResponse?.Invoke(this, data.Result); } } break; case nameof(GetMyFriends): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, List <User> >) }); GetMyFriendsResponse?.Invoke(this, data.Result); } } break; case nameof(GetUserInfo): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, User>) }); GetUserInfoResponse?.Invoke(this, data.Result); } } break; case nameof(UpdateUserInfo): { if (Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int>) }); UpdateUserInfoResponse?.Invoke(this, data.Result); } } break; default: break; } } break; case "Notice": { switch (package.Method) { case "AddFriend": { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(Tuple <int, User, User>) }); FriendAddedNotice?.Invoke(this, data.Result); } break; //好友登录广播地址 case "FriendLoginNotice": { if (!Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Username = default(string), Address = default(string) });; FriendLoginNotice?.Invoke(this, new Tuple <string, string>(data.Username, data.Address)); } } break; case "Logout": { if (!Sessions.ContainsKey(package.SessionID)) { var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Result = default(int), UserName = default(string) }); LogoutResponse?.Invoke(this, new Tuple <int, string>(data.Result, data.UserName)); } } break; } } break; case "Chat": { switch (package.Method) { case "Text": var data = JsonConvert.DeserializeAnonymousType(package.Data, new { Username = default(string), Message = default(string) }); ChatReceived?.Invoke(this, new Tuple <string, string>(data.Username, data.Message)); break; default: break; } } break; default: break; } }