データ通信

using SoftGear.Strix.Client.Core;
using SoftGear.Strix.Client.Match.Room.Model;
using SoftGear.Strix.Client.Room;
using SoftGear.Strix.Client.Room.Message;
using SoftGear.Strix.Net.Serialization;
using SoftGear.Strix.Unity.Runtime;
using SoftGear.Strix.Unity.Runtime.Event;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StrixRPC : MonoBehaviour
{

    public class CustomMessage
    {
        public int value = 0;
        public string text = "";
    }
    public CustomMessage customMessage;

    public class CustomMessage2
    {
        public int value2 = 0;
        public string text2 = "";
    }
    public CustomMessage2 customMessage2;

    // ネットワークに繋げる前にやる サーバーを1度抜けて入りなおす場合にもやる
    void Start()
    {
        StrixNetwork.instance.roomSession.roomClient.RoomRelayNotified += OnRoomRelayNotified;
        // 送受信したいクラスを登録
        ObjectFactory.Instance.Register(typeof(CustomMessage));
        ObjectFactory.Instance.Register(typeof(CustomMessage2));
        // クラスを初期化する
        customMessage = new CustomMessage();
        customMessage2 = new CustomMessage2();
    }

    // サーバーから抜ける
    public void OnDestroyServer()
    {
        StrixNetwork.instance.Destroy();
    }

    // サーバーの状態を取得
    public void OnStatus()
    {
        CustomizableMatchRoom room = StrixNetwork.instance.roomSession.room;
        // ルームサーバーに接続してるか否か
        if (StrixNetwork.instance.roomSession.IsConnected)
        {
            // オーナーか否か
            Debug.Log(StrixNetwork.instance.isRoomOwner);
            // ルームの現在人数/最大人数
            Debug.Log(room.GetMemberCount() + "/" + room.GetCapacity());
            // ルームに居るメンバーの名前
            for (int i = 0; i < StrixNetwork.instance.sortedRoomMembers.Count; i++)
            {
                CustomizableMatchRoomMember member = StrixNetwork.instance.sortedRoomMembers[i];
                Debug.Log(member.GetName());
            }
        }
        else
        {
            Debug.Log("ルームサーバーに接続していません");
        }
    }

    // データ送信
    public void OnTransmission()
    {
        customMessage.value++;
        customMessage.text = "Hello";
        StrixNetwork.instance.SendRoomRelay(customMessage, args => { }, args => { });
    }

    // データ送信その2
    public void OnTransmission2()
    {
        customMessage2.value2++;
        customMessage2.text2 = "Hello2";
        StrixNetwork.instance.SendRoomRelay(customMessage2, args => { }, args => { });
    }

    // データ受信
    private void OnRoomRelayNotified(NotificationEventArgs<RoomRelayNotification> notification)
    {
        object message = notification.Data.GetMessageToRelay();

        if (message is CustomMessage)
        {
            customMessage = (CustomMessage)message;
            Debug.Log("OnRoomRelayNotified CustomMessage { value =  " + customMessage.value + " text = " + customMessage.text + " } ");
        }
        else if (message is CustomMessage2)
        {
            customMessage2 = (CustomMessage2)message;
            Debug.Log("OnRoomRelayNotified CustomMessage { value =  " + customMessage2.value2 + " text = " + customMessage2.text2 + " } ");
        }
    }
}

OnTransmissionを実行する事でクラスCustomMessageを
OnTransmission2を実行する事でクラスCustomMessage2を送信をする事ができます

戻る