123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- #if !BESTHTTP_DISABLE_SOCKETIO
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using BestHTTP.SocketIO;
- using BestHTTP.Examples;
- public sealed class SocketIOWePlaySample : MonoBehaviour
- {
-
-
-
- enum States
- {
- Connecting,
- WaitForNick,
- Joined
- }
-
-
-
- private string[] controls = new string[] { "left", "right", "a", "b", "up", "down", "select", "start" };
-
-
-
- private const float ratio = 1.5f;
-
-
-
- private int MaxMessages = 50;
-
-
-
- private States State;
-
-
-
- private Socket Socket;
-
-
-
- private string Nick = string.Empty;
-
-
-
- private string messageToSend = string.Empty;
-
-
-
- private int connections;
-
-
-
- private List<string> messages = new List<string>();
-
-
-
- private Vector2 scrollPos;
-
-
-
- private Texture2D FrameTexture;
- #region Unity Events
- void Start()
- {
-
- SocketOptions options = new SocketOptions();
- options.AutoConnect = false;
-
- var manager = new SocketManager(new Uri("http://io.weplay.io/socket.io/"), options);
-
- Socket = manager.Socket;
-
- Socket.On(SocketIOEventTypes.Connect, OnConnected);
- Socket.On("joined", OnJoined);
- Socket.On("connections", OnConnections);
- Socket.On("join", OnJoin);
- Socket.On("move", OnMove);
- Socket.On("message", OnMessage);
- Socket.On("reload", OnReload);
-
-
- Socket.On("frame", OnFrame, false);
-
- Socket.On(SocketIOEventTypes.Error, OnError);
-
- manager.Open();
-
- State = States.Connecting;
- }
- void OnDestroy()
- {
-
- Socket.Manager.Close();
- }
- void Update()
- {
-
- if (Input.GetKeyDown(KeyCode.Escape))
- SampleSelector.SelectedSample.DestroyUnityObject();
- }
- void OnGUI()
- {
- switch(State)
- {
- case States.Connecting:
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
- GUILayout.BeginVertical();
- GUILayout.FlexibleSpace();
- GUIHelper.DrawCenteredText("Connecting to the server...");
- GUILayout.FlexibleSpace();
- GUILayout.EndVertical();
- });
- break;
- case States.WaitForNick:
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
- DrawLoginScreen();
- });
- break;
- case States.Joined:
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
-
- if (FrameTexture != null)
- GUILayout.Box(FrameTexture);
- DrawControls();
- DrawChat();
- });
- break;
- }
- }
- #endregion
- #region Helper Functions
-
-
-
- void DrawLoginScreen()
- {
- GUILayout.BeginVertical();
- GUILayout.FlexibleSpace();
- GUIHelper.DrawCenteredText("What's your nickname?");
- Nick = GUILayout.TextField(Nick);
- if (GUILayout.Button("Join"))
- Join();
- GUILayout.FlexibleSpace();
- GUILayout.EndVertical();
- }
- void DrawControls()
- {
- GUILayout.BeginHorizontal();
- GUILayout.Label("Controls:");
- for (int i = 0; i < controls.Length; ++i)
- if (GUILayout.Button(controls[i]))
- Socket.Emit("move", controls[i]);
- GUILayout.Label(" Connections: " + connections);
- GUILayout.EndHorizontal();
- }
- void DrawChat(bool withInput = true)
- {
- GUILayout.BeginVertical();
-
- scrollPos = GUILayout.BeginScrollView(scrollPos, false, false);
- for (int i = 0; i < messages.Count; ++i)
- GUILayout.Label(messages[i], GUILayout.MinWidth(Screen.width));
- GUILayout.EndScrollView();
- if (withInput)
- {
- GUILayout.Label("Your message: ");
- GUILayout.BeginHorizontal();
- messageToSend = GUILayout.TextField(messageToSend);
- if (GUILayout.Button("Send", GUILayout.MaxWidth(100)))
- SendMessage();
- GUILayout.EndHorizontal();
- }
- GUILayout.EndVertical();
- }
-
-
-
-
- void AddMessage(string msg)
- {
- messages.Insert(0, msg);
- if (messages.Count > MaxMessages)
- messages.RemoveRange(MaxMessages, messages.Count - MaxMessages);
- }
-
-
-
- void SendMessage()
- {
- if (string.IsNullOrEmpty(messageToSend))
- return;
- Socket.Emit("message", messageToSend);
- AddMessage(string.Format("{0}: {1}", Nick, messageToSend));
- messageToSend = string.Empty;
- }
-
-
-
- void Join()
- {
- PlayerPrefs.SetString("Nick", Nick);
- Socket.Emit("join", Nick);
- }
-
-
-
- void Reload()
- {
- FrameTexture = null;
- if (Socket != null)
- {
- Socket.Manager.Close();
- Socket = null;
- Start();
- }
- }
- #endregion
- #region SocketIO Events
-
-
-
- private void OnConnected(Socket socket, Packet packet, params object[] args)
- {
- if (PlayerPrefs.HasKey("Nick"))
- {
- Nick = PlayerPrefs.GetString("Nick", "NickName");
- Join();
- }
- else
- State = States.WaitForNick;
- AddMessage("connected");
- }
-
-
-
- private void OnJoined(Socket socket, Packet packet, params object[] args)
- {
- State = States.Joined;
- }
-
-
-
- private void OnReload(Socket socket, Packet packet, params object[] args)
- {
- Reload();
- }
-
-
-
- private void OnMessage(Socket socket, Packet packet, params object[] args)
- {
- if (args.Length == 1)
- AddMessage(args[0] as string);
- else
- AddMessage(string.Format("{0}: {1}", args[1], args[0]));
- }
-
-
-
- private void OnMove(Socket socket, Packet packet, params object[] args)
- {
- AddMessage(string.Format("{0} pressed {1}", args[1], args[0]));
- }
-
-
-
- private void OnJoin(Socket socket, Packet packet, params object[] args)
- {
- string loc = args.Length > 1 ? string.Format("({0})", args[1]) : string.Empty;
- AddMessage(string.Format("{0} joined {1}", args[0], loc));
- }
-
-
-
- private void OnConnections(Socket socket, Packet packet, params object[] args)
- {
- connections = Convert.ToInt32(args[0]);
- }
-
-
-
- private void OnFrame(Socket socket, Packet packet, params object[] args)
- {
- if (State != States.Joined)
- return;
- if (FrameTexture == null)
- {
- FrameTexture = new Texture2D(0, 0, TextureFormat.RGBA32, false);
- FrameTexture.filterMode = FilterMode.Point;
- }
-
- byte[] data = packet.Attachments[0];
-
-
-
-
-
- FrameTexture.LoadImage(data);
- }
-
-
-
- private void OnError(Socket socket, Packet packet, params object[] args)
- {
- AddMessage(string.Format("--ERROR - {0}", args[0].ToString()));
- }
- #endregion
- }
- #endif
|