GitHub | Documentation | Become a Patron

Cant get Unity client to connect to server



  • Hi, I am trying to build a chat system with unity3d but I can't seem to get client to join the room.

    What am I doing wrong?

    Here is my code:
    `

      using UnityEngine;
      using System.Collections;
      using System.Collections.Generic;
      using System;
      using Colyseus;
      using UnityEngine.UI;
    
      using GameDevWare.Serialization;
      using GameDevWare.Serialization.MessagePack;
    
      public class ColyseusClient : MonoBehaviour {
    
        Client client;
        Room room;
    
        public InputField input;
        public Button button;
    
        public string serverName = "localhost";
        public string port = "8080";
        public string roomName = "chat";
    
        // map of players
        Dictionary<string, GameObject> players = new Dictionary<string, GameObject>();
    
        // Use this for initialization
        IEnumerator Start () {
          String uri = "ws://" + serverName + ":" + port;
          Debug.Log (uri);
          client = new Client(uri);
          client.OnOpen += OnOpenHandler;
          client.OnClose += OnCloseHandler;
    
          Debug.Log ("Let's connect the client!");
          yield return StartCoroutine(client.Connect());
    
          Debug.Log ("Let's join the room!");
    
          room = client.Join(roomName, new Dictionary<string, object>() {
            { "create", true }
          });
    
          room.OnReadyToConnect += (sender, e) => {
            Debug.Log("Ready to connect to room!");
            StartCoroutine (room.Connect ());
          };
    
          room.OnJoin += OnRoomJoinHandler;
          room.OnMessage += OnMessageHandler;
    
        }
    
        void OnOpenHandler (object sender, EventArgs e) {
          Debug.Log("Connected to server. Client id: " + client.id);
        }
    
        void OnCloseHandler (object sender, EventArgs e) {
          Debug.Log ("Connection closed");
        }
    
        void OnRoomJoinHandler (object sender, EventArgs e) {
          Debug.Log("Joined room successfully.");
        }
    
        void OnMessageHandler (object sender, MessageEventArgs e) {
          var message = (IndexedDictionary<string, object>) e.message;
          Debug.Log(e.message);
        }
    
    
        void OnApplicationQuit() {
          // Make sure client will disconnect from the server
          room.Leave ();
          client.Close ();
        }
      }
    

    `