fork download
  1. using UnityEngine; //This gives us monobehaviour
  2. using UnityEngine.UI; //This gives us UI classes
  3. using System.Collections; //Known
  4. using System.Collections.Generic; //Known
  5. using UnityEngine.Networking; //This gives us NetworkBehaviour and it's derived methods
  6. using UnityEngine.Networking.NetworkSystem; //This gives us the MsgTypes
  7.  
  8. //Declaring the namespace all my scripts are found under
  9. namespace Panzerwolf
  10. {
  11. //Declaring public class
  12. //This will be directly attached to a physical object in the game,
  13. //and thus derives from UnityEngine.MonoBehaviour
  14.  
  15. //If you see functions here that aren't being called through code,
  16. //they are being called through UI elements in the program
  17. public class gameChatClient : MonoBehaviour
  18. {
  19. public int xoffset = 0;
  20.  
  21. //Unity stuff
  22. [SerializeField] InputField input;
  23.  
  24.  
  25. [SerializeField]Transform chatWindow;
  26. [SerializeField]Transform tabBar;
  27.  
  28. [SerializeField]GameObject textPrefab;
  29. [SerializeField]GameObject tabPrefab;
  30.  
  31. //The timer that will count down towards a timeout
  32. float serverConnectTimeoutCounter = 11;
  33.  
  34. //The timeout limit that the counter will reset to
  35. public int timeoutLimit = 10;
  36.  
  37.  
  38. //Self explanatory by now
  39. public string chatServerHost = "127.0.0.1";
  40. public int chatServerPort = 9999;
  41. public string chatName = "someDude";
  42. public ChatPerson me;
  43.  
  44. //See previous explanations
  45. public Dictionary<ChatChannelId, ChatChannel> channels = new Dictionary<ChatChannelId, ChatChannel>();
  46. public Dictionary<ChatPersonId, ChatPerson> people = new Dictionary<ChatPersonId, ChatPerson>();
  47. public List<bool> channelTabs = new List<bool> ();
  48.  
  49.  
  50. //NetworkClients are required to connect to NetworkServers(well, duh!)
  51. //It is set to null by default, and created using code later.
  52. NetworkClient client = null;
  53.  
  54. //This is the identifier I use for the code to know which channel is the active one
  55. public ChatChannelId chatchannelint;
  56.  
  57. public string talkText = "Hello!";
  58.  
  59. const int kMaxChannelMessages = 5;
  60.  
  61. //The start function is called when an object is instantiated in Unity
  62. void Start()
  63. {
  64. //Call the Setup function
  65. Setup ();
  66. }
  67.  
  68. //Setup the client
  69. void Setup()
  70. {
  71. //Make client a new instance of type NetworkClient
  72. client = new NetworkClient();
  73.  
  74. //Register all the handlers for NetworkMessages
  75. //When the client receives a NetworkMessage,
  76. //it will check the MsgType id (short int) of the NetworkMessage
  77. //and compare it to its own check.
  78.  
  79. //If the comparison returns true, the delegate method hook is called
  80.  
  81. //The rest should with the given explanation explain itself
  82. client.RegisterHandler(MsgType.Connect, OnConnect);
  83. client.RegisterHandler(ChatMsg.Login, OnLogin);
  84. client.RegisterHandler(ChatMsg.ChannelJoin, OnChannelJoin);
  85. client.RegisterHandler(ChatMsg.ChannelLeave, OnChannelLeave);
  86. client.RegisterHandler(ChatMsg.Talk, OnTalk);
  87. client.RegisterHandler(ChatMsg.ChannelFailed, OnChannelJoinFailed);
  88.  
  89.  
  90. //Connect the client using the variables chatServerHost and chatServerPort
  91. client.Connect (chatServerHost, chatServerPort);
  92.  
  93. //Log to console
  94. Debug.Log ("Client set up, trying to connect...");
  95.  
  96. //Set the timer to 0 so it begins counting up towards 10
  97. serverConnectTimeoutCounter = 0;
  98. }
  99.  
  100. //Hook methods are explained under Handlers.
  101. //These get called by their corresponding handler.
  102. void OnConnect(NetworkMessage netMsg)
  103. {
  104. //We have established a connection, now log in to the server
  105. //Call the login method
  106. Login();
  107. }
  108.  
  109. void OnChannelJoinFailed(NetworkMessage netMsg)
  110. {
  111. Debug.LogError ("Failed to find channel! Check spelling and try again.");
  112. }
  113.  
  114. void OnLogin(NetworkMessage netMsg)
  115. {
  116. //Decode the encrypted NetworkMessage using a reader of the correct type
  117. var msg = netMsg.ReadMessage<LoginResponseMessage>();
  118.  
  119. //Give the clients instance of ChatPerson a name and an ID from the server
  120. me = new ChatPerson(msg.personName, msg.personId);
  121.  
  122. //Add the client to his own people list
  123. people[me.personId] = me;
  124.  
  125. //Here I've setup a select bunch of default channels
  126. //the client will join automatically
  127. JoinChannel ("Global");
  128. JoinChannel ("Local");
  129. JoinChannel ("Party");
  130. JoinChannel ("Blades of Urdual");
  131. }
  132.  
  133. void OnChannelJoin(NetworkMessage netMsg)
  134. {
  135. //See above
  136. var msg = netMsg.ReadMessage<ChannelJoinResponseMessage>();
  137.  
  138. Debug.Log ("OnChannelJoin called");
  139.  
  140. //Declare TEMPORARY variable instance.
  141. //This instance will only exist inside this method
  142. ChatChannel channel;
  143.  
  144. //Check if we already have the channel on our list
  145. if (channels.ContainsKey(msg.channelId))
  146. {
  147. //Set the channel to join equal to the channel on the list
  148. channel = channels[msg.channelId];
  149. }
  150.  
  151. //if not
  152. else
  153. {
  154. //Set channel to be equal to a new client version reference instance copy of the channel from the server
  155. channel = new ChatChannel(msg.channelName, msg.channelId, msg.channelColor, msg.channelAlias);
  156.  
  157. //Add it to list of joined channels
  158. channels[channel.channelId] = channel;
  159. }
  160.  
  161.  
  162. //Check if people contains the person joining already
  163. if (!people.ContainsKey(msg.personId))
  164. {
  165. //if not make a new reference instance copy
  166. var newPerson = new ChatPerson(msg.personName, msg.personId);
  167. people[newPerson.personId] = newPerson;
  168. }
  169.  
  170. //Set temporary variable instance
  171. var person = people[msg.personId];
  172. channel.ClientAdd(person);
  173.  
  174. //If the channelname in question is "Global"
  175. if(msg.channelName == "Global")
  176. {
  177. //Set it to be the currently active one
  178. chatchannelint = msg.channelId;
  179. }
  180. //This will only happen once per gameChatClient run
  181. //and servers as the way to make Global the default chat
  182. //(as we cannot have it be null)
  183.  
  184. //Check if the client is the one joining the channel
  185. if(msg.personId == me.personId)
  186. {
  187. //Unity stuff for GUI
  188. if (!GameObject.Find (msg.channelName + " channel"))
  189. {
  190. GameObject tab = Instantiate (tabPrefab);
  191. tab.name = msg.channelName + " channel";
  192. tab.transform.SetParent (tabBar);
  193. tab.transform.localScale = new Vector3 (1, 1, 1);
  194. tab.transform.GetChild (0).GetComponent<Text> ().text = msg.channelAlias;
  195. tab.GetComponent<Button> ().onClick.AddListener (() => ChangeChannel (msg.channelId));
  196. }
  197. }
  198.  
  199. //**NOT USED**
  200. //This was for another version of the ChatClient that could leave and join channels at will
  201. // leaveChannel.SetActive (true);
  202. // joinChannel.SetActive (false);
  203.  
  204. //Log to console
  205. Debug.Log("Client joined channel " + msg.channelId + " " + person.personId);
  206. }
  207.  
  208. void OnChannelLeave(NetworkMessage netMsg)
  209. {
  210. //See above
  211. var msg = netMsg.ReadMessage<ChannelLeaveResponseMessage>();
  212. //chatChannelId = msg.channelId;
  213.  
  214. //See above
  215. ChatChannel channel;
  216.  
  217. //See above
  218. if (channels.ContainsKey(msg.channelId))
  219. {
  220. //See above
  221. channel = channels[msg.channelId];
  222. }
  223.  
  224. //See above
  225. else
  226. {
  227. //See above
  228. Debug.LogError("Leave channel not found:" + msg.channelId);
  229. return;
  230. }
  231.  
  232. //See above
  233. if (msg.personId == me.personId)
  234. {
  235. //Remove channel from clients channel list
  236. channels.Remove(msg.channelId);
  237.  
  238. //See above
  239. Debug.Log("Client left channel " + msg.channelId );
  240. return;
  241. }
  242.  
  243. //See above
  244. ChatPerson person;
  245.  
  246. //See above
  247. if (people.ContainsKey(msg.personId))
  248. {
  249. person = people[msg.personId];
  250. }
  251.  
  252. //See above
  253. else
  254. {
  255. Debug.LogError("Leave person not found:" + msg.personId);
  256. return;
  257. }
  258.  
  259. //Remove the client from the channel
  260. channel.ClientRemove(person);
  261. Debug.Log("Other left channel " + msg.channelId + " " + person.personId);
  262. }
  263.  
  264.  
  265.  
  266. void OnTalk(NetworkMessage netMsg)
  267. {
  268. //See above
  269. var msg = netMsg.ReadMessage<TalkMessage>();
  270.  
  271. //See above
  272. var channel = channels[msg.channelId];
  273.  
  274. //See above
  275. var person = channel.people[msg.personId];
  276.  
  277. //Early debugging, I still keep it cause it looks cool in the console lol
  278. Debug.Log("Client Talk: [" + channel.channelName + "] " + person.personName + ": " + msg.text);
  279.  
  280. //Add the message to the specified channels list of messages
  281. channel.messages.Add(msg);
  282.  
  283. //If the total count of messages exceeds the maxcount
  284. if (channel.messages.Count > kMaxChannelMessages)
  285. {
  286. //Remove the oldest message
  287. channel.messages.RemoveAt(0);
  288. }
  289.  
  290.  
  291. //Draw the messages in Unity
  292. DrawMsg ();
  293. }
  294.  
  295.  
  296. //Regular methods
  297. public void Login()
  298. {
  299. //Successful Login, set the timer above 10 so it stops counting
  300. serverConnectTimeoutCounter = 11;
  301.  
  302. //Create a new NetworkMessage of type LoginMessage
  303. var msg = new LoginMessage();
  304. msg.personName = chatName;
  305.  
  306. //client.Send will send to the server alone
  307. client.Send(ChatMsg.Login, msg);
  308. Debug.Log("Client login");
  309. }
  310.  
  311.  
  312.  
  313. //This is all Unity Stuff
  314. void ChangeChannel(ChatChannelId channelid)
  315. {
  316. for(int i = 0; i < chatWindow.childCount; i++)
  317. {
  318. Destroy (chatWindow.GetChild (i).gameObject);
  319. }
  320.  
  321. GameObject.Find (channels [chatchannelint].channelName + " channel").GetComponent<Image> ().color = Color.white;
  322. chatchannelint = channelid;
  323. GameObject.Find (channels [chatchannelint].channelName + " channel").GetComponent<Image> ().color = Color.gray;
  324. // Debug.Log ("ChangeChannel called");
  325.  
  326.  
  327. //Draws old messages
  328. foreach(var t in channels[chatchannelint].messages)
  329. {
  330. string Name = channels [chatchannelint].people [t.personId].personName + " msg nr " + channels[channelid].messages.FindIndex (x => x.text == t.text).ToString ();
  331. if (GameObject.Find (Name))
  332. Destroy (GameObject.Find (Name));
  333.  
  334. GameObject text = Instantiate (textPrefab);
  335. text.name = Name;
  336. text.transform.SetParent (chatWindow);
  337. text.transform.GetChild (0).GetChild (0).GetComponent<Text> ().text = channels [chatchannelint].people [t.personId].personName;
  338. text.transform.GetChild (1).GetComponent<Text> ().text = t.text;
  339. text.transform.GetChild (1).GetComponent<Text> ().color = Color.grey;
  340.  
  341. }
  342.  
  343. }
  344.  
  345.  
  346. void Update()
  347. {
  348. if(serverConnectTimeoutCounter <= 10)
  349. {
  350. serverConnectTimeoutCounter += Time.deltaTime;
  351. if (serverConnectTimeoutCounter > 10 && serverConnectTimeoutCounter < 11)
  352. {
  353. Debug.LogError ("Connect failed, are you sure you have the right IP/PORT?");
  354. StopClient ();
  355. serverConnectTimeoutCounter = 11;
  356. }
  357. }
  358. }
  359.  
  360. //**NOT USED**
  361. //Also used for the previous client version where client could start and stop NetworkClient at will
  362. // public void StartClient()
  363. // {
  364. // if (serverInput.text == string.Empty)
  365. // {
  366. // Debug.LogError ("Enter an IP adress!");
  367. // return;
  368. // }
  369. //
  370. // if(portInput.text == string.Empty)
  371. // {
  372. // Debug.LogError("Enter a port!");
  373. // return;
  374. // }
  375. //
  376. // stopClient.SetActive (true);
  377. // nameInput.gameObject.SetActive (false);
  378. // serverInput.gameObject.SetActive (false);
  379. // portInput.gameObject.SetActive (false);
  380. // startClient.SetActive (false);
  381. // Setup ();
  382. //
  383. // }
  384.  
  385. public void StopClient()
  386. {
  387. //For each channel
  388. foreach(var ch in channels.Values)
  389. {
  390. //Leave the channel(ServerSide)
  391. ch.LeaveServer (me);
  392. }
  393.  
  394. //Leave all channels(ClientSide)
  395. LeaveChannel ();
  396.  
  397. //Reset timeout counter to above 10 so it doesn't count
  398. serverConnectTimeoutCounter = 11;
  399.  
  400. //**NOT USED**
  401. //Other client version
  402. // nameInput.gameObject.SetActive (true);
  403. // stopClient.SetActive (false);
  404. // serverInput.gameObject.SetActive (true);
  405. // portInput.gameObject.SetActive (true);
  406. // channelInput.gameObject.SetActive (false);
  407. // joinChannel.SetActive (false);
  408. // startClient.SetActive (true);
  409.  
  410. //Disconnect the client from the server
  411. client.Disconnect();
  412.  
  413. //Reset the client instance to null
  414. client = null;
  415. }
  416.  
  417. // public void JoinChannel()
  418. // {
  419. // Debug.Log ("JoinChannel called");
  420. // var response = new ChannelJoinRequestMessage ();
  421. //
  422. // response = new ChannelJoinRequestMessage ();
  423. // response.name = channelInput.text;
  424. // response.personId = myChatPerson.personId;
  425. //
  426. // client.connection.Send (myChatMsg.ChannelJoin, response);
  427. // channelInput.text = string.Empty;
  428. // Debug.Log("Client Login myChatPerson " + myChatPerson.personId);
  429. // }
  430.  
  431.  
  432. //You should be able to understand this one now
  433. public void JoinChannel(string name)
  434. {
  435. Debug.Log ("JoinChannel by name called");
  436. var response = new ChannelJoinRequestMessage ();
  437.  
  438. response = new ChannelJoinRequestMessage ();
  439. response.name = name;
  440. response.personId = me.personId;
  441.  
  442. client.connection.Send (ChatMsg.ChannelJoin, response);
  443. Debug.Log("Client Login myChatPerson " + me.personId);
  444. }
  445.  
  446. //You should be able to understand this one too now
  447. public void LeaveChannel()
  448. {
  449. Debug.Log ("Left channels");
  450. foreach(var c in channels.Keys)
  451. {
  452. var leaveMsg = new ChannelLeaveRequestMessage();
  453. leaveMsg.channelId = c;
  454. leaveMsg.personId = me.personId;
  455.  
  456. client.Send(ChatMsg.ChannelLeave, leaveMsg);
  457. }
  458. }
  459.  
  460.  
  461. //Draw the messages on screen
  462. void DrawMsg()
  463. {
  464. //Set temporary variable
  465. ChatChannel curchan = channels [chatchannelint];
  466.  
  467. //For each message T in the current channels list of messages
  468. foreach(var t in curchan.messages)
  469. {
  470. //If client is currently connected to the channel
  471. if(channels.ContainsKey(t.channelId))
  472. {
  473. var channel = channels [t.channelId];
  474. var person = channel.people [t.personId];
  475.  
  476. string Name = person.personName + " msg nr " + t.count;
  477.  
  478. //This is just to prevent multiple in-game text GameObjects of the same message
  479. //If the script can find a GameObject that is active with a name that matches the Name variable
  480. if (GameObject.Find (Name))
  481. {
  482.  
  483. }
  484.  
  485. //if not
  486. else
  487. {
  488. //Make a temporary GameObject reference
  489. GameObject text = Instantiate (textPrefab);
  490. //Set it equal to a new instance of a textPrefabrication(Unity stuff)
  491.  
  492. //Set the name of the GameObject in the scene
  493. text.name = Name;
  494.  
  495. //Set the parent of the transform of the gameObject (this will anchor it to the parent object)
  496. text.transform.SetParent (chatWindow);
  497.  
  498. //Get the Script Component "Text" that is attached to the Child of the Child of the GameObject
  499. //and set it's text value to be equal to the name of the person that wrote the message
  500. text.transform.GetChild (0).GetChild (0).GetComponent<Text> ().text = person.personName;
  501.  
  502. //Set the text of the Text component on the Child to be equal to the message.text value
  503. text.transform.GetChild (1).GetComponent<Text> ().text = t.text;
  504.  
  505. //Set the color of the Text component on the Child to be equal to the color of the channel of the message
  506. text.transform.GetChild (1).GetComponent<Text> ().color = channel.channelColor;
  507. }
  508. }
  509. }
  510.  
  511. }
  512.  
  513.  
  514. //Send NetworkMessage for talk
  515. public void SendTalk()
  516. {
  517. //Declare temporary variable reference and set it equal to the currently active channel
  518. ChatChannel curChan = channels [chatchannelint];
  519.  
  520.  
  521. //Get the text from InputField input (Unity stuff)
  522. talkText = input.text;
  523. if (talkText == null)
  524. //If the talktext is null, return from the function(why would we want to send one of these)
  525. return;
  526.  
  527.  
  528. //This should be known by now
  529. var talkMsg = new TalkMessage();
  530. talkMsg.personId = me.personId;
  531. talkMsg.channelId = curChan.channelId;
  532. talkMsg.text = talkText;
  533.  
  534. //Reset the input text to empty
  535. input.text = string.Empty;
  536.  
  537.  
  538. //Send the message to the server
  539. client.Send(ChatMsg.Talk, talkMsg);
  540.  
  541. }
  542. }
  543. }
  544.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(1,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(2,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(5,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(6,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(17,32): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(22,20): error CS0246: The type or namespace name `InputField' could not be found. Are you missing an assembly reference?
prog.cs(25,19): error CS0246: The type or namespace name `Transform' could not be found. Are you missing an assembly reference?
prog.cs(26,19): error CS0246: The type or namespace name `Transform' could not be found. Are you missing an assembly reference?
prog.cs(28,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(29,19): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(42,10): error CS0246: The type or namespace name `ChatPerson' could not be found. Are you missing an assembly reference?
prog.cs(45,21): error CS0246: The type or namespace name `ChatChannelId' could not be found. Are you missing an assembly reference?
prog.cs(45,36): error CS0246: The type or namespace name `ChatChannel' could not be found. Are you missing an assembly reference?
prog.cs(46,21): error CS0246: The type or namespace name `ChatPersonId' could not be found. Are you missing an assembly reference?
prog.cs(46,35): error CS0246: The type or namespace name `ChatPerson' could not be found. Are you missing an assembly reference?
prog.cs(52,3): error CS0246: The type or namespace name `NetworkClient' could not be found. Are you missing an assembly reference?
prog.cs(55,10): error CS0246: The type or namespace name `ChatChannelId' could not be found. Are you missing an assembly reference?
prog.cs(102,18): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(109,28): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(114,16): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(133,22): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(208,23): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(266,15): error CS0246: The type or namespace name `NetworkMessage' could not be found. Are you missing an assembly reference?
prog.cs(314,22): error CS0246: The type or namespace name `ChatChannelId' could not be found. Are you missing an assembly reference?
Compilation failed: 24 error(s), 0 warnings
stdout
Standard output is empty