public void Connect(string roomName) { if (Status == SourceStatus.CONNECTING) { Disconnect(); } if (string.IsNullOrWhiteSpace(HostChannelName)) { logger.LogError("HostChannelName is empty"); return; } while (string.IsNullOrWhiteSpace(SOAuth)) { var result = RequestSetup(); if (result == false) { return; } } if (currentIRCIO != null) { currentIRCIO.DisConnect(); currentIRCIO.OnRecieveRawMessage -= onRecieveRawMessage; currentIRCIO = null; } try { currentIRCIO = new TwitchIRCIO(roomName) { OAuth = SOAuth, ChannelName = HostChannelName, ClientID = ClientID }; currentIRCIO.Connect(); currentIRCIO.OnRecieveRawMessage += onRecieveRawMessage; currentIRCIO.OnError += CurrentIRCIO_OnError; RaiseEvent(new BaseStatusEvent(SourceStatus.CONNECTED_WORKING)); UpdateChannelViewersCount(); viewerUpdateTimer = new Timer(viewersUpdateInterval); viewerUpdateTimer.Elapsed += (z, zz) => UpdateChannelViewersCount(); viewerUpdateTimer.Start(); Status = SourceStatus.CONNECTED_WORKING; } catch (Exception e) { logger.LogError("twitch source connect error!" + e.Message); Status = SourceStatus.USER_DISCONNECTED; } }
public override void Disconnect() { currentIRCIO?.DisConnect(); currentIRCIO = null; RaiseEvent(new BaseStatusEvent(SourceStatus.USER_DISCONNECTED)); viewerUpdateTimer?.Stop(); viewerUpdateTimer?.Dispose(); }
public void Connect(string roomName) { channelName = roomName; if (channelName.Length == 0) { IO.CurrentIO.WriteColor("频道名不能为空!", ConsoleColor.Red); return; } while (oauth.Length == 0) { var result = RequestSetup(); if (result == false) { return; } } SaveConfig(); if (currentIRCIO != null) { currentIRCIO.DisConnect(); currentIRCIO.OnRecieveRawMessage -= onRecieveRawMessage; currentIRCIO = null; } try { currentIRCIO = new TwitchIRCIO(roomName) { OAuth = oauth, ChannelName = channelName, ClientID = clientId }; currentIRCIO.Connect(); currentIRCIO.OnRecieveRawMessage += onRecieveRawMessage; RaiseEvent(new BaseStatusEvent(SourceStatus.CONNECTED_WORKING)); UpdateChannelViewersCount(); viewerUpdateTimer = new Timer(viewersUpdateInterval); viewerUpdateTimer.Elapsed += (z, zz) => UpdateChannelViewersCount(); viewerUpdateTimer.Start(); } catch (Exception e) { IO.CurrentIO.WriteColor("twitch connect error!" + e.Message, ConsoleColor.Red); } }
private void CurrentIRCIO_OnError(TwitchIRCIO arg1, Exception arg2) { if (arg1 != currentIRCIO) { return; } logger.LogError($"IRC kernel occured exception:\"{arg2.Message}\",try to reconnect."); try { ReConnect(); } catch (Exception e) { logger.LogError($"can't reconnet:\"{e}\"."); RaiseEvent(new BaseStatusEvent(SourceStatus.USER_DISCONNECTED)); } }