Example #1
0
    public static void Connect(string host, string username, bool toggleWss)
    {
        print(string.Format("Connecting to server: {0} ...", host));

        WebsocketSharpFactory socketFactory = new WebsocketSharpFactory();

        socket = new Socket(socketFactory, new Socket.Options
        {
            channelRejoinInterval = TimeSpan.FromMilliseconds(200),
            //logger = new BasicLogger()
        });

        socket.OnOpen = () => {
            LobbyChannel.JoinLobby(username);
        };
        socket.OnError = (x) => {
            MenuServerConnect.joinError             = "error_connecting";
            MenuServerConnect.connectedSuccessfully = false;
            MenuServerConnect.connectionDone        = true;
        };
        string socketUrlBase = "ws://{0}/socket";

        if (toggleWss)
        {
            socketUrlBase = "wss://{0}/socket";
        }
        socket.Connect(string.Format(socketUrlBase, host), null);
    }
Example #2
0
    // Start is called before the first frame update
    void Start()
    {
        _queue         = new Queue <QueueMessage>();
        _controllQueue = new Queue <Controll>();
        _players       = new Dictionary <string, Player>();
        _id            = GenerateRandomID();

        var factory = new WebsocketSharpFactory();
        var socket  = new Socket(factory);

        socket.Connect("ws://localhost:4000/socket", new Dictionary <string, string>()
        {
            { "token", _id },
        });

        var channel = socket.MakeChannel("room:lobby");

        channel.On("on_connect", m => {
            _queue.Enqueue(new QueueMessage("on_connect", m.payload));
        });
        channel.On("player_joined", m => {
            _queue.Enqueue(new QueueMessage("player_joined", m.payload));
        });
        channel.On("player_leave", m => {
            _queue.Enqueue(new QueueMessage("player_leave", m.payload));
        });
        channel.On("movement", m => {
            _queue.Enqueue(new QueueMessage("movement", m.payload));
        });

        channel.Join();
        _ch = channel;

        // Create button bindings
        buttonLeft.onClick.AddListener(MoveLeft);
        buttonRight.onClick.AddListener(MoveRight);
        buttonTop.onClick.AddListener(MoveTop);
        buttonDown.onClick.AddListener(MoveDown);
    }