Exemplo n.º 1
0
    void Start()
    {
        GameObject go = GameObject.Find("SpacebrewObject");

        sbEvents = go.GetComponent <SpacebrewEvents> ();
        light    = GameObject.Find("ScaryImage/InnerDetails/node_Hypercolor_e8ef32b1-baa8-460a-9c2c-9cf8506794f5_0_0");
        StartCoroutine(timeFunctionforLightOnOff());
    }
Exemplo n.º 2
0
    void Awake()
    {
        spacebrewNewMsgs = new List <SpacebrewMessage>();
        spacebrewMsgs    = new List <SpacebrewMessage>();

////////
        print("CLIENT - AWAKE");
//TODO: check values for pubilshers & subscribers message types!
        print("types: " + messageTypes.Count);
        for (int i = 0; i < messageTypes.Count; ++i)
        {
            print(messageTypes[i]);
        }
////////

        sbEvents = gameObject.GetComponentInParent <SpacebrewEvents>();
        if (sbEvents == null)
        {
            throw new Exception("SpacebrewClient: SpacebrewEvent not found in parent object!");
        }

        if (isServer && useDefaultPort)
        {
            serverPort = DEFAULT_SERVER_PORT;
        }
        if (!isServer && useConfigManager)
        {
            print("Getting server address and port from Server Configuration Manager");

            // find a ServerManager
            IServerManager serverManager      = null;
            var            listServerManagers = FindObjectsOfType <MonoBehaviour>().OfType <IServerManager>();
            if ((listServerManagers == null) || (listServerManagers.Count() == 0))
            {
                throw new Exception("A SERVER MANAGER IMPLEMENTATION MUST EXIST IN THE SCENE IF THE SERVER ADDRESS IS NOT SPECFIED IN THE EDITOR!");
            }
            serverManager = listServerManagers.First();

            serverAddress = serverManager.GetServerAddress();
            serverPort    = serverManager.GetServerPort();
        }
//TODO: check that "serverAddress" & "serverPort" have valid format!
//...

        string connectionString = SERVER_ADDRESS_STRING.Replace("<ADDRESS>", serverAddress).Replace("<PORT>", serverPort);

        print("Server address: " + connectionString);

        if (isAdmin)
        {
            sbAdmin = gameObject.GetComponentInParent <SpacebrewAdmin>();
            if (sbAdmin == null)
            {
                throw new Exception("SpacebrewClient: SpacebrewAdmin not found in parent object! - Required if 'Is Admin' is set to 'true'");
            }
            print("Admin client");

            var config = new Config {
                clientName    = clientName,
                description   = descriptionText,
                publishers    = new List <Publisher>(publishers),
                subscribers   = new List <Subscriber>(subscribers),
                remoteAddress = ""
            };
            sbAdmin.SetConfig(config);
        }

        if (isServer)
        {
            sbServer = gameObject.GetComponentInParent <NodeJs>();
            if (sbServer == null)
            {
                throw new Exception("SpacebrewClient: NodeJs not found in parent object! - Required if 'Is Server' is set to 'true'");
            }

            print("Starting Spacebrew server");
            sbServer.Init();
            sbServer.Run();

            //wait?
            //...
        }

        conn = new WebSocket(connectionString);

        conn.OnOpen += (sender, e) => {
            print("Attempting to open socket");
        };

        if (isAdmin)
        {
            conn.OnMessage += (sender, e) => {
//TODO: REMOVE THIS PRINT - DEBUG PURPOSE
//print ("onMessage: " + e.Data);

                // parse the incoming json message from spacebrew
                var N = JSON.Parse(e.Data);

                JSONNode targetType = N["targetType"];
                JSONNode message    = N["message"];
                if ((targetType == null) && (message != null))
                {
                    HandleMessage(message);
                }
                else
                {
                    if (targetType.ToString() == "\"admin\"")
                    {
                        HandleAdminMessage(N);
                    }
                    else
                    {
                        JSONNode route = N[0]["route"];
                        if (route != null)
                        {
//TODO: REMOVE THIS PRINT - DEBUG PURPOSE
                            print("remove-route message - disconnected client");
                            HandleRouteMessage(route);
                        }
                        else
                        {
                            //TODO: handle other messages?
                            print("Not admin message - unhandled");
                        }
                    }
                }
            };
        }
        else
        {
            conn.OnMessage += (sender, e) => {
//TODO: REMOVE THIS PRINT - DEBUG PURPOSE
//print ("onMessage: " + e.Data);

                // parse the incoming json message from spacebrew
                var N = JSON.Parse(e.Data);

                JSONNode message = N["message"];
                if (message != null)
                {
                    HandleMessage(message);
                }

//TODO: remove this commented code... (use?)
//              if (e.Type == Opcode.Text) {
//                  // Do something with e.Data
//                  print (e);
//                  print (e.Data);
//                  return;
//              }
//
//              if (e.Type == Opcode.Binary) {
//                  // Do something with e.RawData
//                  return;
//              }
            };
        }

        conn.OnError += (sender, e) => {
            print("THERE WAS AN ERROR CONNECTING");
            print(e.Message);
        };

        conn.OnClose += (sender, e) => {
            print("Connection closed");
        };


        print("Attemping to connect to " + connectionString);
        conn.Connect();

        //addPublisher ("power", "boolean", "0");
        //addSubscriber ("hits", "boolean");

        // Special message to Spacebrew to act as admin interface
        if (isAdmin)
        {
            print("Sending admin request");
            conn.Send(ADMIN_REQUEST_MESSAGE);
        }

//TODO: check if still need to send this message when sending the previous admin one
        // Connect and send the configuration for the app to Spacebrew
//		conn.Send (makeConfig().ToString());
        SendConfigMessage();
    }