Bonjour tout le monde
J'ai besoin d'aide avec mon appli de chat que j'ai faite sous unity.
j'ai suivi un tuto sur youtube qui est très clair et qui fonctionne bien. Sauf que la deco ne fonctionne pas bien et le tuto ne parle pas de ça.
Voici l'erreur que j'obtiens quand je jeux me deco
ObjectDisposedException: The object was used after being disposed.
System.Net.Sockets.TcpClient.CheckDisposed ()
System.Net.Sockets.TcpClient.GetStream ()
Server.Update () (at Assets/Script/Server/Server.cs:62)
ALors evidemment, j'ai cherché de mon cote mais je ne trouve pas de solutions.
J'ai bien compris que c'est à la ligne 62 que j'ai un pb et que c'est lié à la récupération d'un flux d'info. Mais il me semble pourtant que je ferme bien le flux quand je me deco, alors bon je sais pas trop quoi faire.
Merci par avance à la personne qui prendra le temps de 'décoder' tout ça et de m'apporter un peu de lumière.
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 using UnityEngine; using System.Collections; using System.Net.Sockets; using System.Collections.Generic; using System; using System.Net; using System.IO; public class Server : MonoBehaviour { private List<ServerClient> clients; private List<ServerClient> disconnectedList; public string ip = "127.0.0.1"; public int portnb = 9865; private TcpListener server; private bool serverStarted; 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; foreach(ServerClient c in clients) { // Client tjrs connecté ? if(!IsConnected(c.tcp)) { c.tcp.Close(); disconnectedList.Add(c); return; } // Verif si message envoyé par client else { NetworkStream s = c.tcp.GetStream(); if (s.DataAvailable) { StreamReader reader = new StreamReader(s, true); string data = reader.ReadLine(); if(data != null) OnIncomingData(c, data); } } // Affiche tableau des users connectés // Debug.Log("SERVEUR tableau users : " + clients.Count +"\t - Name = " + c.clientName + "\t - DECO = " + disconnectedList.Count); // Message 'User' s'est déconnecté for (int i = 0; i < disconnectedList.Count - 1; i++) { Broadcast(disconnectedList[i].clientName + " s'est déconnecté !", clients); clients.Remove(disconnectedList[i]); disconnectedList.RemoveAt(i); } // Affiche tableau des users connectés Debug.Log("SERVEUR tableau users : " + clients.Count + "\t - Name = " + c.clientName + "\t - DECO = " + disconnectedList.Count); } } 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; // empeche 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); 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 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 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 string h; // HostInput int p; // PortInput string l; // Login 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; } catch(Exception e) { Debug.Log("CLIENT.CS - Fonction ConnectToServer Socket Error : " + e.Message + " IP = " + host + " et Port = " + port ); } } /* public void DisconnectFromServer() { try { // GameObject.Find("Deconnexion").GetComponent<Button>().OnClick.RemoveAllListeners(); // GameObject.Find("Deconnexion").GetComponent<Button>().OnClick.AddListener(MonoBehaviour.singleton.StopHost); // network = new NetworkConnection(); // network.Disconnect(); Network.CloseConnection(Network.connections[1], true); } catch(Exception e) { Debug.Log("error message : " + e.Message); } } */ 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; //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 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