Explorar el Código

去重

ZhangXueYang
zhangxueyang hace 3 años
padre
commit
8453c253c9
Se han modificado 2 ficheros con 182 adiciones y 147 borrados
  1. 1
    1
      XHZB.Desktop/ToolbarWindow.xaml.cs
  2. 181
    146
      XHZB.Desktop/WebSocket/WSocketClient.cs

+ 1
- 1
XHZB.Desktop/ToolbarWindow.xaml.cs Ver fichero

@@ -204,7 +204,7 @@ namespace XHZB.Desktop
204 204
                     }
205 205
                     else
206 206
                     {
207
-                        //APP.W_RollCallWindow.Initialize();
207
+                        APP.W_RollCallWindow.Initialize();
208 208
                     }
209 209
                     APP.W_RollCallWindow.Owner = this;
210 210
                     //ZSocketServer.getInstance().addWin(rollCallWindow);

+ 181
- 146
XHZB.Desktop/WebSocket/WSocketClient.cs Ver fichero

@@ -125,41 +125,50 @@ namespace XHZB.Desktop.WebSocket
125 125
 
126 126
             SocketModel msgBean = JsonHelper.JsonToObj<SocketModel>(e.Message);
127 127
 
128
-           //if(isMsg)
129
-           // {
130
-           //     GetOnlineStudentsModel msgBean = JsonHelper.JsonToObj<GetOnlineStudentsModel>(e.Message);
131
-           //     if (msgBean.c == 1040)
132
-           //     {
128
+            //if(isMsg)
129
+            // {
130
+            //     GetOnlineStudentsModel msgBean = JsonHelper.JsonToObj<GetOnlineStudentsModel>(e.Message);
131
+            //     if (msgBean.c == 1040)
132
+            //     {
133 133
 
134
-           //     }
135
-           // }
134
+            //     }
135
+            // }
136 136
 
137 137
             if (msgBean != null && msgBean.b != null)
138 138
             {
139
-                if(msgBean.c==1040)
139
+                if (msgBean.c == 1040)
140 140
                 {
141
-                    GetOnlineStudentsModel msgOnline= JsonHelper.JsonToObj<GetOnlineStudentsModel>(e.Message);
141
+                    GetOnlineStudentsModel msgOnline = JsonHelper.JsonToObj<GetOnlineStudentsModel>(e.Message);
142 142
 
143
-                    foreach(StudentModel getOnline in msgOnline.b.stulst)
143
+                    foreach (StudentModel getOnline in msgOnline.b.stulst)
144 144
                     {
145
-                        OnlineUserModel item = new OnlineUserModel
145
+
146
+                        OnlineUserModel item = new OnlineUserModel();
147
+                        if (getUserByIds(getOnline.userid) == null)
146 148
                         {
147
-                            usertype = 2001,
148
-                            userid = getOnline.userid,
149
-                            username = getOnline.username,
150
-                            userpic = getOnline.headportrait
151
-                        };
152
-                        APP.OnlineUserList.Add(item);
153
-                    }
149
+                         
150
+                            item.usertype = 2001;
151
+                            item.userid = getOnline.userid;
152
+                            item.username = getOnline.username;
153
+                            item.userpic = getOnline.headportrait;
154
+                            APP.OnlineUserList.Add(item);
155
+                        }
154 156
 
155
-                    //GetOnlineStudentsModel get = new GetOnlineStudentsModel();
156
-                    //get.b = new StulstModel();
157
+                    }
157 158
                    
158
-                    //Console.WriteLine("WS:消息收到1111:" + get);
159
+                    return;
159 160
                 }
160
-                if (msgBean.c != 0&& msgBean.u == 2)
161
+               
162
+                //GetOnlineStudentsModel get = new GetOnlineStudentsModel();
163
+                //get.b = new StulstModel();
164
+
165
+                //Console.WriteLine("WS:消息收到1111:" + get);
166
+            }
167
+            if (msgBean.c != 0 && msgBean.u == 2)
168
+            {
169
+                if (msgBean.c == 2001)//上线
161 170
                 {
162
-                    if (msgBean.c == 2001)//上线
171
+                    if(getUserByIds(msgBean.b.stid) == null)
163 172
                     {
164 173
                         OnlineUserModel item = new OnlineUserModel
165 174
                         {
@@ -171,172 +180,198 @@ namespace XHZB.Desktop.WebSocket
171 180
                         APP.OnlineUserList.Add(item);
172 181
                         sendUserChangeToWin();
173 182
                     }
174
-                    else if(msgBean.c == 2002)//下线
183
+                 
184
+                   
185
+                }
186
+                else if (msgBean.c == 2002)//下线
187
+                {
188
+                    try
175 189
                     {
176
-                        try
190
+                        for (int i = 0; i < APP.OnlineUserList.Count; i++)
177 191
                         {
178
-                            for (int i = 0; i < APP.OnlineUserList.Count; i++)
192
+                            if (msgBean.b.stid == APP.OnlineUserList[i].userid)
179 193
                             {
180
-                                if (msgBean.b.stid == APP.OnlineUserList[i].userid)
181
-                                {
182
-                                    APP.OnlineUserList.RemoveAt(i);
183
-                                  
184
-                                    break;
185
-                                }
194
+                                APP.OnlineUserList.RemoveAt(i);
195
+
196
+                                break;
186 197
                             }
187 198
                         }
188
-                        catch (Exception ex)
189
-                        {
190
-                            LogHelper.WriteErrLog("(移除)" + ex.Message, ex);
191
-                        }
192
-                        sendUserChangeToWin();
193 199
                     }
194
-                    sendMsgToWin(msgBean);
200
+                    catch (Exception ex)
201
+                    {
202
+                        LogHelper.WriteErrLog("(移除)" + ex.Message, ex);
203
+                    }
204
+                    sendUserChangeToWin();
195 205
                 }
206
+                sendMsgToWin(msgBean);
196 207
             }
197 208
         }
198
-        private static void sendMsgToWin(SocketModel msg)
209
+  
210
+    /// <summary>
211
+    /// 是否存在用户
212
+    /// </summary>
213
+    /// <param name="userid"></param>
214
+    /// <returns></returns>
215
+    public OnlineUserModel getUserByIds(int userid)
216
+    {
217
+        if (APP.OnlineUserList.Count > 0)
199 218
         {
200
-            foreach (KeyValuePair<Type, object> kvp in WinDic)
219
+            OnlineUserModel[] userList = APP.OnlineUserList.ToArray();
220
+            foreach (OnlineUserModel user in userList)
201 221
             {
202
-                if (kvp.Value is SocketCallback callback)
222
+                if (user != null)
203 223
                 {
204
-                    callback.receiveWsMsg(msg);
224
+                    if (user.userid == userid)
225
+                    {
226
+                        return user;
227
+                    }
205 228
                 }
206 229
             }
207 230
         }
208
-        /// <summary>
209
-        /// 添加要推送消息的窗口
210
-        /// </summary>
211
-        /// <param name="win"></param>
212
-        public void addWin(object win)
231
+        return null;
232
+    }
233
+    private static void sendMsgToWin(SocketModel msg)
234
+    {
235
+        foreach (KeyValuePair<Type, object> kvp in WinDic)
213 236
         {
214
-            if (WinDic.ContainsKey(win.GetType()))
237
+            if (kvp.Value is SocketCallback callback)
215 238
             {
216
-                WinDic[win.GetType()] = win;
217
-            }
218
-            else
219
-            {
220
-                WinDic.Add(win.GetType(), win);
239
+                callback.receiveWsMsg(msg);
221 240
             }
222 241
         }
223
-        /// <summary>
224
-        /// 发送给需要接收的窗口
225
-        /// </summary>
226
-        private static void sendUserChangeToWin()
242
+    }
243
+    /// <summary>
244
+    /// 添加要推送消息的窗口
245
+    /// </summary>
246
+    /// <param name="win"></param>
247
+    public void addWin(object win)
248
+    {
249
+        if (WinDic.ContainsKey(win.GetType()))
227 250
         {
228
-            foreach (KeyValuePair<Type, object> kvp in WinDic)
229
-            {
230
-                if (kvp.Value is SocketCallback callback)
231
-                {
232
-                    callback.userListChange();
233
-                }
234
-            }
251
+            WinDic[win.GetType()] = win;
235 252
         }
236
-        /// <summary>
237
-        /// 移除窗口
238
-        /// </summary>
239
-        /// <param name="win"></param>
240
-        public void removedWin(object win)
253
+        else
254
+        {
255
+            WinDic.Add(win.GetType(), win);
256
+        }
257
+    }
258
+    /// <summary>
259
+    /// 发送给需要接收的窗口
260
+    /// </summary>
261
+    private static void sendUserChangeToWin()
262
+    {
263
+        foreach (KeyValuePair<Type, object> kvp in WinDic)
241 264
         {
242
-            if (WinDic.ContainsKey(win.GetType()))
265
+            if (kvp.Value is SocketCallback callback)
243 266
             {
244
-                WinDic.Remove(win.GetType());
267
+                callback.userListChange();
245 268
             }
246 269
         }
270
+    }
271
+    /// <summary>
272
+    /// 移除窗口
273
+    /// </summary>
274
+    /// <param name="win"></param>
275
+    public void removedWin(object win)
276
+    {
277
+        if (WinDic.ContainsKey(win.GetType()))
278
+        {
279
+            WinDic.Remove(win.GetType());
280
+        }
281
+    }
247 282
 
248 283
 
249 284
 
250 285
 
251
-        /// <summary>
252
-        /// Socket关闭事件
253
-        /// </summary>
254
-        /// <param name="sender"></param>
255
-        /// <param name="e"></param>
256
-        void WebSocket_Closed(object sender, EventArgs e)
257
-        {
258
-            _Logger.Info("websocket_Closed");
259
-        }
260
-        /// <summary>
261
-        /// Socket报错事件
262
-        /// </summary>
263
-        /// <param name="sender"></param>
264
-        /// <param name="e"></param>
265
-        void WebSocket_Error(object sender, ErrorEventArgs e)
266
-        {
267
-            _Logger.Info("websocket_Error:" + e.Exception.ToString());
268
-        }
269
-        /// <summary>
270
-        /// Socket打开事件
271
-        /// </summary>
272
-        /// <param name="sender"></param>
273
-        /// <param name="e"></param>
274
-        void WebSocket_Opened(object sender, EventArgs e)
275
-        {
276
-            SendMessage(SocketMsgManger.AddMsg());
277
-            _Logger.Info(" websocket_Opened");
286
+    /// <summary>
287
+    /// Socket关闭事件
288
+    /// </summary>
289
+    /// <param name="sender"></param>
290
+    /// <param name="e"></param>
291
+    void WebSocket_Closed(object sender, EventArgs e)
292
+    {
293
+        _Logger.Info("websocket_Closed");
294
+    }
295
+    /// <summary>
296
+    /// Socket报错事件
297
+    /// </summary>
298
+    /// <param name="sender"></param>
299
+    /// <param name="e"></param>
300
+    void WebSocket_Error(object sender, ErrorEventArgs e)
301
+    {
302
+        _Logger.Info("websocket_Error:" + e.Exception.ToString());
303
+    }
304
+    /// <summary>
305
+    /// Socket打开事件
306
+    /// </summary>
307
+    /// <param name="sender"></param>
308
+    /// <param name="e"></param>
309
+    void WebSocket_Opened(object sender, EventArgs e)
310
+    {
311
+        SendMessage(SocketMsgManger.AddMsg());
312
+        _Logger.Info(" websocket_Opened");
278 313
 
279 314
 
280 315
 
281
-            SendMessage(SocketMsgManger.offlineMsg());
316
+        SendMessage(SocketMsgManger.offlineMsg());
282 317
 
283
-        }
284
-        /// <summary>
285
-        /// 检查重连线程
286
-        /// </summary>
287
-        private void CheckConnection()
318
+    }
319
+    /// <summary>
320
+    /// 检查重连线程
321
+    /// </summary>
322
+    private void CheckConnection()
323
+    {
324
+        do
288 325
         {
289
-            do
326
+            try
290 327
             {
291
-                try
328
+                if (this._webSocket.State != WebSocket4Net.WebSocketState.Open && this._webSocket.State != WebSocket4Net.WebSocketState.Connecting)
292 329
                 {
293
-                    if (this._webSocket.State != WebSocket4Net.WebSocketState.Open && this._webSocket.State != WebSocket4Net.WebSocketState.Connecting)
294
-                    {
295
-                        _Logger.Info(" Reconnect websocket WebSocketState:" + this._webSocket.State);
296
-                        this._webSocket.Close();
297
-                        this._webSocket.Open();
298
-                        Console.WriteLine("正在重连");
299
-                    }
330
+                    _Logger.Info(" Reconnect websocket WebSocketState:" + this._webSocket.State);
331
+                    this._webSocket.Close();
332
+                    this._webSocket.Open();
333
+                    Console.WriteLine("正在重连");
300 334
                 }
301
-                catch (Exception ex)
302
-                {
303
-                    _Logger.Error(ex.ToString());
304
-                }
305
-                System.Threading.Thread.Sleep(5000);
306
-            } while (this._isRunning);
307
-        }
308
-        #endregion
309
-
310
-        /// <summary>
311
-        /// 发送消息
312
-        /// </summary>
313
-        /// <param name="Message"></param>
314
-        public void SendMessage(string Message)
315
-        {
316
-            Task.Factory.StartNew(() =>
335
+            }
336
+            catch (Exception ex)
317 337
             {
318
-                if (_webSocket != null && _webSocket.State == WebSocket4Net.WebSocketState.Open)
319
-                {
320
-                    this._webSocket.Send(Message);
321
-                    Console.WriteLine("WS:发送消息:" + Message);
322
-                }
323
-            });
324
-        }
338
+                _Logger.Error(ex.ToString());
339
+            }
340
+            System.Threading.Thread.Sleep(5000);
341
+        } while (this._isRunning);
342
+    }
343
+    #endregion
325 344
 
326
-        public void Dispose()
345
+    /// <summary>
346
+    /// 发送消息
347
+    /// </summary>
348
+    /// <param name="Message"></param>
349
+    public void SendMessage(string Message)
350
+    {
351
+        Task.Factory.StartNew(() =>
327 352
         {
328
-            this._isRunning = false;
329
-            try
353
+            if (_webSocket != null && _webSocket.State == WebSocket4Net.WebSocketState.Open)
330 354
             {
331
-                _thread.Abort();
355
+                this._webSocket.Send(Message);
356
+                Console.WriteLine("WS:发送消息:" + Message);
332 357
             }
333
-            catch
334
-            {
358
+        });
359
+    }
360
+
361
+    public void Dispose()
362
+    {
363
+        this._isRunning = false;
364
+        try
365
+        {
366
+            _thread.Abort();
367
+        }
368
+        catch
369
+        {
335 370
 
336
-            }
337
-            this._webSocket.Close();
338
-            this._webSocket.Dispose();
339
-            this._webSocket = null;
340 371
         }
372
+        this._webSocket.Close();
373
+        this._webSocket.Dispose();
374
+        this._webSocket = null;
341 375
     }
342 376
 }
377
+}

Loading…
Cancelar
Guardar