/// <summary> /// Send the user athentication info to the chat server. /// </summary> /// <see cref="https://dev.mixer.com/reference/chat/methods/auth"/> /// <param name="authToken">The chat auth token</param> /// <param name="channelId">The channel to connect to chat for</param> /// <param name="userId">The user the auth token is for</param> /// <param name="token">A cancellation token</param> /// <returns>void</returns> private async Task Authenticate(string authToken, uint channelId, uint userId, CancellationToken token) { Messages.SendMethodCall methodData = new Messages.SendMethodCall { method = ChatMethod.Auth, arguments = new JArray(channelId, userId, authToken), id = Interlocked.Increment(ref this.messageId), }; await this.chatSocket.SendAsync(SerializeToJsonBytes(methodData), WebSocketMessageType.Text, true, token); // the response may not be the next message Messages.AuthenticationReply reply = null; while (reply == null) { byte[] nextMessage = await this.RecieveMessageAsync(token); reply = DeserializeMessage <Messages.AuthenticationReply>(nextMessage); if (string.CompareOrdinal(reply.type, Messages.BaseReply.Type) != 0 || reply.id != methodData.id) { // not our message, wait for the next one reply = null; } } if (reply.error != null) { throw new WebSocketException(reply.error.code, reply.error.message); } }
/// <summary> /// Call to do the socket send. /// </summary> /// <param name="method">The method to be sent</param> /// <param name="parameters">Parameters to the method</param> /// <returns>Async task</returns> private async Task SendToChatAsync(string method, JArray parameters) { using (var ts = new CancellationTokenSource()) { Messages.SendMethodCall methodData = new Messages.SendMethodCall { method = method, arguments = parameters, id = Interlocked.Increment(ref this.messageId), }; await this.chatSocket.SendAsync(SerializeToJsonBytes(methodData), WebSocketMessageType.Text, true, ts.Token); // at this point you'd want to validate the response, but we're throwing them away // in the recieve for simplicity, so we'll skip it } }