Salut tt le monde
Qui pourrait me donner un conseil pour réussir à afficher le contenu d'une liste dans un 'panel'
J'essaie d'instancier un objet texte qui correspond au user qui vient de se connecter.
Mais quand j'utilise la meme methode que pour les messages j'ai l'erreur suivante
UnassignedReferenceException: The variable connectedUserContainer of Server has not been assigned.
You probably need to assign the connectedUserContainer variable of the Server script in the inspector.
Server.Update () (at Assets/Script/Server/Server.cs:79)
C'est bizarre parce que je n'ai pas l'impression d'avoir assigner chatContainer mais je l'ai peut etre fait à l'insu de mon plein gre
bref
Merci par avance
Serveur
CLIENT
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Net.Sockets; using System.Collections.Generic; using System; using System.Net; using System.IO; public class Server : MonoBehaviour { public List<ServerClient> clients; private List<ServerClient> disconnectedList; public string ip = "127.0.0.1"; public int portnb = 9865; private TcpListener server; private bool serverStarted; //public string user; public GameObject usernamePrefab; public GameObject connectedUserContainer; private void Start() { clients = new List<ServerClient>(); disconnectedList = new List<ServerClient>(); try { server = new TcpListener(IPAddress.Any, portnb); server.Start(); StartListening(); serverStarted = true; Debug.Log("Le serveur a démarré sur le port " + portnb.ToString()); // Debug.Log("Le serveur a démarré sur le port " + port); // Debug.Log("IP est " + ip); } catch (Exception e) { Debug.Log("SERVER.CS - Fonction Start Socket Error : " + e.Message); } } private void Update() { if (!serverStarted) return; for (int i=0; i<clients.Count; i++) // Boucle pour les connectés { ServerClient cl = clients[i]; // Affiche tableau des users connectés //Debug.Log("Name = " + cl.clientName); // usernamePrefab = cl.clientName; if (!IsConnected(cl.tcp)) // Si plus de connexion { //Debug.Log(cl.clientName+" s'est deco"); Broadcast(cl.clientName + " s'est deco", clients); Image OnOff = GameObject.Find("OnOff").GetComponent<Image>(); OnOff.color = Color.red; cl.tcp.Close(); disconnectedList.Add(cl); //return; clients.RemoveAt(i); i--; } // Verif si message envoyé par client else { NetworkStream s = cl.tcp.GetStream(); if (s.DataAvailable) { StreamReader reader = new StreamReader(s, true); string data = reader.ReadLine(); if(data != null) OnIncomingData(cl, data); } } } // Message 'User' s'est déconnecté for (int i = 0; i < disconnectedList.Count - 1; i++) { Broadcast(disconnectedList[i].clientName + " s'est déconnecté !", clients); } disconnectedList.Clear(); /* foreach(ServerClient c in clients) { Debug.Log("ttt "+c.clientName); } */ } private void StartListening() { server.BeginAcceptTcpClient(AcceptTcpClient, server); } private bool IsConnected(TcpClient c) { try { if(c != null && c.Client != null && c.Client.Connected) { if (c.Client.Poll(0, SelectMode.SelectRead)) { return !(c.Client.Receive(new byte[1], SocketFlags.Peek) == 0); } return true; } } catch { return false; } return true; // corrige erreur : not all code paths return a value } private void AcceptTcpClient(IAsyncResult ar) { TcpListener listener = (TcpListener)ar.AsyncState; clients.Add(new ServerClient(listener.EndAcceptTcpClient(ar))); StartListening(); // Envoi message à tt le monde : nouvelle connexion // Broadcast(clients[clients.Count - 1].clientName + " s'est connecté", clients); Broadcast("%NAME",new List<ServerClient>() { clients[clients.Count - 1]}); } private void OnIncomingData(ServerClient c, string data) { //Debug.Log(c.clientName + " a écrit : " + data); if(data.Contains("&NAME")) { c.clientName = data.Split('|')[1]; Broadcast(c.clientName + " s'est connecté.", clients); //GameObject go = Instantiate(usernamePrefab, connectedUserContainer.transform) as GameObject; return; } Broadcast(c.clientName + " : " + data, clients); } private void Broadcast(string data, List<ServerClient> cl) { foreach(ServerClient c in cl) { try { StreamWriter writer = new StreamWriter(c.tcp.GetStream()); writer.WriteLine(data); writer.Flush(); } catch(Exception e) { Debug.Log("BROADCAST - Message d'erreur : " + e.Message + " pour client : " + c.clientName); } } } } public class ServerClient { public TcpClient tcp; public string clientName; public ServerClient(TcpClient clientSocket) { clientName = "Invité"; tcp = clientSocket; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using System.Net.Sockets; using System.IO; using System; public class Client : MonoBehaviour { private bool socketReady; private TcpClient socket; private NetworkStream stream; private StreamWriter writer; private StreamReader reader; private NetworkConnection network; public string clientName; public NetworkPlayer networkClient; public GameObject chatContainer; public GameObject messagePrefab; public GameObject usernamePrefab; public GameObject connectedUserContainer; public string h; // HostInput public int p; // PortInput public string l; // Login public string user; public void ConnectToServer() { // Si deja connecté, ignorez cette methode if (socketReady) return; // IP et port par défaut string host = "127.0.0.1"; int port = 9865; //string login = ""; // Remplace les valeurs par défaut par celle saisies dans les champs h = GameObject.Find("HostInput").GetComponent<InputField>().text; if (h != "") host = h; l = GameObject.Find("Login").GetComponent<InputField>().text; if (l != "") clientName = l; //networkClient = l; // Debug.Log("Login = " + login + "ou L = "+l); int.TryParse(GameObject.Find("PortInput").GetComponent<InputField>().text, out p); if (port != 0) //port = p; // On créé la connexion try { socket = new TcpClient(host, port); stream = socket.GetStream(); writer = new StreamWriter(stream); reader = new StreamReader(stream); socketReady = true; // Changement de couleur du voyant de connexion Image OnOff = GameObject.Find("OnOff").GetComponent<Image>(); OnOff.color = Color.green; } catch(Exception e) { Debug.Log("CLIENT.CS - Fonction ConnectToServer Socket Error : " + e.Message + " IP = " + host + " et Port = " + port ); } } private void Update () { if (socketReady) { if(stream.DataAvailable) { string data = reader.ReadLine(); if(data != null) { OnIncomingData(data); } } } } private void OnIncomingData(string data) { if(data == "%NAME") { Send("&NAME|" + clientName); return; } GameObject go = Instantiate(messagePrefab, chatContainer.transform) as GameObject; go.GetComponentInChildren<Text>().text = data; // chatContainer.transform.position = new Vector2(0, 0); //GameObject goo = Instantiate(messagePrefab, chatContainer.transform.position) as GameObject; //Debug.Log("CLIENT.CS - Server : " + data); } private void Send(string data) { if (!socketReady) return; writer.WriteLine(data); writer.Flush(); } public void OnSendButton() { string message = GameObject.Find("EnvoiTexte").GetComponent<InputField>().text; Send(message); } public void OnEnter() { string message = GameObject.Find("EnvoiTexte").GetComponent<InputField>().text; if (Input.GetKeyDown(KeyCode.Return)|| Input.GetKeyDown(KeyCode.KeypadEnter)) { Send(message); } } public void CloseSocket() { if (!socketReady) return; // Network.CloseConnection(networkClient, true); stream.Close(); writer.Close(); reader.Close(); socket.Close(); socketReady = false; } private void OnApplicationQuit() { CloseSocket(); } private void OnDisable() { CloseSocket(); } }
Partager