public void StartListen(Action <ChatExchange> receiveCommand, Action <ChatExchange> commandCommand, Action <Exception> ExceptionAction = null) { try { bool doReceive = true; while (doReceive) { byte[] receiveBuffer = new byte[1024 * 2]; int receiveSize = socket.Receive(receiveBuffer); if (expectingCommandResponse) { CommandResponseHandler(CommandExchangeResponse.Decode(receiveBuffer, receiveSize)); expectingCommandResponse = false; } else { ChatExchange chatExchange = ChatExchange.Decode(receiveBuffer, receiveSize); switch (chatExchange.ExchangeType) { case ChatExchangeType.Message: receiveCommand?.Invoke(chatExchange); break; case ChatExchangeType.Command: if (chatExchange.Exchange == "LEFT") { doReceive = false; } commandCommand?.Invoke(chatExchange); continue; default: break; } } } } catch (SocketException e) { ExceptionAction?.Invoke(e); } }
public void CommandResponseHandler(CommandExchangeResponse response) { switch (response.ResponseType) { case CommandResponse.Success: chatPage.OutputTextSafe(string.Format("[Server] {0}", response.OutputMessage)); break; case CommandResponse.UnknownCommand: chatPage.OutputTextSafe("Unknown command. Type '/help' for a list of commands."); break; case CommandResponse.Fail: chatPage.OutputTextSafe(string.Format("[Server] {0}", response.OutputMessage)); break; default: chatPage.OutputTextSafe("Error executing command."); break; } }