private void Update() { while (_events.Count > 0) { ORTCPEventType eventType = _events.Dequeue(); ORTCPEventParams eventParams = new ORTCPEventParams(); eventParams.eventType = eventType; eventParams.client = this; eventParams.socket = _client; if (eventType == ORTCPEventType.Connected) { if (verbose) { print("[{name}] Connnected to server"); } if (serverDelegate != null) { serverDelegate.OnServerConnect(eventParams); } Send(isShowControl?"100":GameManager.StatusMsgBackup); } else if (eventType == ORTCPEventType.Disconnected) { if (verbose) { print("[{name}] Disconnnected from server"); } if (serverDelegate != null) { serverDelegate.OnClientDisconnect(eventParams); } _reader.Close(); _writer.Close(); _client.Close(); if (autoConnectOnDisconnect) { ORTimer.Execute(gameObject, disconnectTryInterval, "OnDisconnectTimer"); } } else if (eventType == ORTCPEventType.DataReceived) { if (socketType == ORTCPSocketType.Text) { eventParams.message = _messages.Dequeue(); if (verbose) { print("[{name}] DataReceived: " + eventParams.message); } if (OnTCPMessageRecived != null) { OnTCPMessageRecived(eventParams); } } else { eventParams.packet = _packets.Dequeue(); } if (serverDelegate != null) { serverDelegate.OnDataReceived(eventParams); } } else if (eventType == ORTCPEventType.ConnectionRefused) { if (verbose) { print("[{name}] ConnectionRefused... will try again..."); } if (autoConnectOnConnectionRefused) { ORTimer.Execute(gameObject, connectionRefusedTryInterval, "OnConnectionRefusedTimer"); } } } }
public void Start() { Connect(); Observable .Interval(TimeSpan.FromSeconds(1)) .Where(x => _events.Count > 0) .Subscribe(x => { ORTCPEventType eventType = _events.Dequeue(); ORTCPEventParams eventParams = new ORTCPEventParams(); eventParams.eventType = eventType; eventParams.client = this; eventParams.socket = _client; if (eventType == ORTCPEventType.Connected) { //Console.WriteLine("[TCPClient] Connnected to server"); if (serverDelegate != null) { serverDelegate.OnServerConnect(eventParams); } } else if (eventType == ORTCPEventType.Disconnected) { //Console.WriteLine("[TCPClient] Disconnnected from server"); if (serverDelegate != null) { serverDelegate.OnClientDisconnect(eventParams); } _reader.Close(); _writer.Close(); _client.Close(); } else if (eventType == ORTCPEventType.DataReceived) { if (socketType == ORTCPSocketType.Text) { eventParams.message = _messages.Dequeue(); // Console.WriteLine("[TCPClient] DataReceived: "+ eventParams.message); if (OnTCPMessageRecived != null) { OnTCPMessageRecived(eventParams); } } else { eventParams.packet = _packets.Dequeue(); } if (serverDelegate != null) { serverDelegate.OnDataReceived(eventParams); } } else if (eventType == ORTCPEventType.ConnectionRefused) { //Console.WriteLine("[TCPClient] ConnectionRefused... will try again..."); } }); }