ChatPeer
How do I use Chat Peer
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ChatClient.cs
Copy
94 public bool Connect(string address, ConnectionProtocol protocol, string appId, string appVersion, string userId, AuthenticationValues authValues)
95 {
96 if (!this.HasPeer)
97 {
98 this.chatPeer = new ChatPeer(this, protocol);
99 }
100 else
101 {
102 this.Disconnect();
103 if (this.chatPeer.UsedProtocol != protocol)
104 {
105 this.chatPeer = new ChatPeer(this, protocol);
106 }
107 }
108
109#if UNITY
110#pragma warning disable 0162 // the library variant defines if we should use PUN's SocketUdp variant (at all)
111 if (PhotonPeer.NoSocket)
112 {
113#if !UNITY_EDITOR && (UNITY_PS3 || UNITY_ANDROID)
114 UnityEngine.Debug.Log("Using class SocketUdpNativeDynamic");
115 this.chatPeer.SocketImplementation = typeof(SocketUdpNativeDynamic);
116#elif !UNITY_EDITOR && UNITY_IPHONE
117 UnityEngine.Debug.Log("Using class SocketUdpNativeStatic");
118 this.chatPeer.SocketImplementation = typeof(SocketUdpNativeStatic);
119#elif !UNITY_EDITOR && (UNITY_WINRT)
120 // this automatically uses a separate assembly-file with Win8-style Socket usage (not possible in Editor)
121#else
122 Type udpSocket = Type.GetType("ExitGames.Client.Photon.SocketUdp, Assembly-CSharp");
123 this.chatPeer.SocketImplementation = udpSocket;
124 if (udpSocket == null)
125 {
126 UnityEngine.Debug.Log("ChatClient could not find a suitable C# socket class. The Photon3Unity3D.dll only supports native socket plugins.");
127 }
128#endif
129 if (this.chatPeer.SocketImplementation == null)
130 {
131 UnityEngine.Debug.Log("No socket implementation set for 'NoSocket' assembly. Please contact Exit Games.");
132 }
133 }
134#pragma warning restore 0162
135#endif
136
137 this.chatPeer.TimePingInterval = 3000;
138 this.DisconnectedCause = ChatDisconnectCause.None;
139
140 this.CustomAuthenticationValues = authValues;
141 this.UserId = userId;
142 this.AppId = appId;
143 this.AppVersion = appVersion;
144 this.didAuthenticate = false;
145 this.msDeltaForServiceCalls = 100;
146
147
148 // clean all channels
149 this.PublicChannels.Clear();
150 this.PrivateChannels.Clear();
151
152 if (!address.Contains(":"))
153 {
154 int port = 0;
155 ProtocolToNameServerPort.TryGetValue(protocol, out port);
156 address = string.Format("{0}:{1}", address, port);
157 }
158
159 bool isConnecting = this.chatPeer.Connect(address, "NameServer");
160 if (isConnecting)
161 {
162 this.State = ChatState.ConnectingToNameServer;
163 }
164 return isConnecting;
165 }
File name: ChatClient.cs
Copy
174 public void Service()
175 {
176 if (this.HasPeer && (Environment.TickCount - msTimestampOfLastServiceCall > msDeltaForServiceCalls || msTimestampOfLastServiceCall == 0))
177 {
178 msTimestampOfLastServiceCall = Environment.TickCount;
179 this.chatPeer.Service(); //TODO: make sure to call service regularly. in best case it could be integrated into PhotonHandler.FallbackSendAckThread()!
180 }
181 }
File name: ChatClient.cs
Copy
183 public void Disconnect()
184 {
185 if (this.HasPeer && this.chatPeer.PeerState != PeerStateValue.Disconnected)
186 {
187 this.chatPeer.Disconnect();
188 }
189 }
File name: ChatClient.cs
Copy
191 public void StopThread()
192 {
193 if (this.HasPeer)
194 {
195 this.chatPeer.StopThread();
196 }
197 }
File name: ChatClient.cs
Copy
270 public bool PublishMessage(string channelName, object message)
271 {
272 if (!this.CanChat)
273 {
274 // TODO: log error
275 return false;
276 }
277
278 if (string.IsNullOrEmpty(channelName) || message == null)
279 {
280 this.LogWarning("PublishMessage parameters must be non-null and not empty.");
281 return false;
282 }
283
284 Dictionary
285 {
286 { (byte)ChatParameterCode.Channel, channelName },
287 { (byte)ChatParameterCode.Message, message }
288 };
289
290 return this.chatPeer.OpCustom((byte)ChatOperationCode.Publish, parameters, true);
291 }
File name: ChatClient.cs
Copy
311 public bool SendPrivateMessage(string target, object message, bool encrypt)
312 {
313 if (!this.CanChat)
314 {
315 // TODO: log error
316 return false;
317 }
318
319 if (string.IsNullOrEmpty(target) || message == null)
320 {
321 this.LogWarning("SendPrivateMessage parameters must be non-null and not empty.");
322 return false;
323 }
324
325 Dictionary
326 {
327 { ChatParameterCode.UserId, target },
328 { ChatParameterCode.Message, message }
329 };
330
331 bool sent = this.chatPeer.OpCustom((byte)ChatOperationCode.SendPrivate, parameters, true, 0, encrypt);
332 return sent;
333 }
File name: ChatClient.cs
Copy
343 /// The message object can be anything that Photon can serialize, including (but not limited to)
350 private bool SetOnlineStatus(int status, object message, bool skipMessage)
351 {
352 if (!this.CanChat)
353 {
354 // TODO: log error
355 return false;
356 }
357
358 Dictionary
359 {
360 { ChatParameterCode.Status, status },
361 };
362
363 if (skipMessage)
364 {
365 parameters[ChatParameterCode.SkipMessage] = true;
366 }
367 else
368 {
369 parameters[ChatParameterCode.Message] = message;
370 }
371 return this.chatPeer.OpCustom(ChatOperationCode.UpdateStatus, parameters, true);
372 }
File name: ChatClient.cs
Copy
433 public bool AddFriends(string[] friends)
434 {
435 if (!this.CanChat)
436 {
437 // TODO: log error
438 return false;
439 }
440
441 if (friends == null || friends.Length == 0)
442 {
443 this.LogWarning("AddFriends can't be called for empty or null list.");
444 return false;
445 }
446
447 Dictionary
448 {
449 { ChatParameterCode.Friends, friends },
450 };
451 return this.chatPeer.OpCustom(ChatOperationCode.AddFriends, parameters, true);
452 }
File name: ChatClient.cs
Copy
496 public bool RemoveFriends(string[] friends)
497 {
498 if (!this.CanChat)
499 {
500 // TODO: log error
501 return false;
502 }
503
504 if (friends == null || friends.Length == 0)
505 {
506 this.LogWarning("RemoveFriends can't be called for empty or null list.");
507 return false;
508 }
509
510 Dictionary
511 {
512 { ChatParameterCode.Friends, friends },
513 };
514 return this.chatPeer.OpCustom(ChatOperationCode.RemoveFriends, parameters, true);
515 }
Download file with original file name:ChatPeer
ChatPeer 90 lượt xem
Gõ tìm kiếm nhanh...