protected override void ChannelRead0(IChannelHandlerContext ctx, object msg) { IChannel ch = ctx.Channel; if (!handshaker.IsHandshakeComplete) { try { handshaker.FinishHandshake(ch, (IFullHttpResponse)msg); completionSource.TryComplete(); var enterRoom = new JObject(new JProperty("roomid", bilibiliLiveNetty.roomid)); var str = enterRoom.ToString(); var enterRoomByteBuffer = Encode(7, str); ctx.WriteAndFlushAsync(new BinaryWebSocketFrame(enterRoomByteBuffer)); UnityTask.RunOnUIThread(() => bilibiliLiveNetty.StartCoroutine(Heartbeat(ctx))); Debug.Log("WebSocket Client connected!"); } catch (WebSocketHandshakeException e) { Debug.Log("WebSocket Client failed to connect"); completionSource.TrySetException(e); } return; } if (msg is IFullHttpResponse response) { throw new InvalidOperationException( $"Unexpected FullHttpResponse (getStatus={response.Status}, content={response.Content.ToString(Encoding.UTF8)})"); } if (msg is TextWebSocketFrame textFrame) { Debug.Log($"WebSocket Client received message: {textFrame.Text()}"); } else if (msg is BinaryWebSocketFrame binaryFrame) { IByteBuffer byteBuffer = binaryFrame.Content; var(operation, body) = Decode(byteBuffer); Handle(operation, body); } else if (msg is PongWebSocketFrame) { Debug.Log("WebSocket Client received pong"); } else if (msg is CloseWebSocketFrame) { Debug.Log("WebSocket Client received closing"); ch.CloseAsync(); } }
// Start is called before the first frame update void Start() { animator = GetComponent <Animator>(); var bilibiliLiveRest = new BilibiliLiveRest("https://api.live.bilibili.com"); BilibiliLiveNetty.Instance.onDanmakuMessage = (uname, message) => { if (message == "打你哟") { UnityTask.RunOnUIThread(() => animator.SetTrigger("DAMAGED00")); } }; BilibiliLiveNetty.Instance.onSendGift = (uname, action, num, giftName) => { UnityTask.RunOnUIThread(() => { StartCoroutine(SendGiftResponse(uname, action, num, giftName, bilibiliLiveRest)); }); }; }
public void Start() { UnityTask.RunOnUIThread(_action); }
void Ready() { foreach (var piece in FindObjectsOfType <Piece>()) { Destroy(piece.gameObject); } int i = 1; foreach (var homeCell in yellowHomeCells) { homeCell.piece = Instantiate(yellowPiecePrefab, homeCell.transform.position, Quaternion.identity); homeCell.piece.index = i; i++; } i = 1; foreach (var homeCell in blueHomeCells) { homeCell.piece = Instantiate(bluePiecePrefab, homeCell.transform.position, Quaternion.identity); homeCell.piece.index = i; i++; } i = 1; foreach (var homeCell in greenHomeCells) { homeCell.piece = Instantiate(greenPiecePrefab, homeCell.transform.position, Quaternion.identity); homeCell.piece.index = i; i++; } i = 1; foreach (var homeCell in redHomeCells) { homeCell.piece = Instantiate(redPiecePrefab, homeCell.transform.position, Quaternion.identity); homeCell.piece.index = i; i++; } state = State.Ready; BilibiliLiveNetty.Instance.onDanmakuMessage = (uname, message) => { if (message.ToLower() == "roll") { UnityTask.RunOnUIThread(() => { if (!rollDict.ContainsKey(uname)) { rollDict[uname] = Random.Range(0, 100); } }); } }; currentCountDown = countDown; rollDict.Clear(); PlayerList.Instance.text.text = ""; Info.Instance.text.text = $"倒计时{currentCountDown}秒,请观众发送弹幕Roll进行Roll点,Roll点最高的4个将进入游戏"; SpeakInfo(); StartCoroutine(ReadyCoroutine()); }