public void OnSendMsgBtn(string msg)
    {
        WebSocketInfo info = new WebSocketInfo();

        info.active = "user.login";
        info.data   = new Dictionary <string, string>();
        info.data.Add("phone", "13560048370");
        info.data.Add("password", "111111");
        //测试数据  测试登录
        if (WebSocketPanel.Instance.ConnectSocket())
        {
            WebSocketPanel.Instance.SendMessage(JsonMapper.ToJson(info), ReceiverMsg);
        }
    }
示例#2
0
文件: Server.cs 项目: bmdub/MaintFace
		private static async void CommandListener(WebSocketInfo webSocketInfo)
		{
			ArraySegment<byte> arr = new ArraySegment<byte>(new byte[1024]);
			CancellationToken ct = new CancellationToken();

			Interlocked.Increment(ref _connectedCount);

			try
			{
				for (;;)
				{
					try
					{
						using (MemoryStream ms = new MemoryStream())
						{
							for (;;)
							{
								if (webSocketInfo.Socket.State != WebSocketState.Open)
									return;
								var res = await webSocketInfo.Socket.ReceiveAsync(arr, ct);
								ms.Write(arr.Array, 0, res.Count);
								if (res.EndOfMessage)
									break;
							}

							var messageSerialized = System.Text.Encoding.Default.GetString(ms.ToArray());

							var serializer = new JavaScriptSerializer();

							var message = (ClientMessage)serializer.Deserialize(messageSerialized, typeof(ClientMessage));
							if (message == null)
								continue;

							if (!string.IsNullOrEmpty(message.CustomButton))
							{
#pragma warning disable 4014
								if (!Options.HasFlag(MaintFaceOptions.EnableButtons))
								{
									Trace.WriteLine(nameof(MaintFace) + ": Button use is disabled.");
								}
								else
								{
									CustomButtonEvent evt;
									if (Buttons.TryGetEvent(message.CustomButton, out evt))
										Task.Run(() =>
										{
											try
											{
												evt.Trigger(message.CustomButton);
											}
											catch (Exception ex)
											{
												Trace.WriteLine(nameof(MaintFace) + " Error: " + nameof(evt.Trigger) + ": " + ex.Message);
											}
										});
								}
#pragma warning restore 4014
							}
							if (!string.IsNullOrEmpty(message.Command))
							{
								if (!Options.HasFlag(MaintFaceOptions.EnableCommands))
								{
									Trace.WriteLine(nameof(MaintFace) + ": Command use is disabled.");
								}
								else
								{
									if (CommandReceived != null)
									{
#pragma warning disable 4014
										_consoleMessageQueue.Enqueue(">" + message.Command);
										var args = new CommandReceivedEventArgs(message.Command);

										Task.Run(() =>
										{
											try
											{
												CommandReceived(null, args);
											}
											catch (Exception ex)
											{
												Trace.WriteLine(nameof(MaintFace) + " Error: " + nameof(CommandReceived) + ": " + ex.Message);
											}
										}).ContinueWith((task) =>
										{
											if (args.Response != null)
												_consoleMessageQueue.Enqueue(args.Response);
										});
									}
								}
#pragma warning restore 4014
							}
							if (message.DumpTracePointName != null)
							{
								TraceNode traceNode;
								if (_nodes.TryGetValue(message.DumpTracePointName, out traceNode))
								{
									AwaitableObject<VariableDump> dumpRequest =
										new AwaitableObject<VariableDump>();
									traceNode.VarDumpRequests.Enqueue(dumpRequest);
									var dump = await dumpRequest.WaitForValue();

									ServerVarDumpMessage response = new ServerVarDumpMessage();
									response.MessageId = message.MessageId;
									response.VarDump = dump;
									var serializedResult = serializer.Serialize(response);
									var buffer = Encoding.UTF8.GetBytes(serializedResult);
									var segment = new ArraySegment<byte>(buffer);
									if (webSocketInfo.Socket.State != WebSocketState.Open)
										return;

									await webSocketInfo.SendAsync(segment);
								}
							}
							if (message.StatOverride != null)
							{
								if (!Options.HasFlag(MaintFaceOptions.EnableVariableOverride))
								{
									Trace.WriteLine(nameof(MaintFace) + ": Variable overrides are disabled.");
								}
								else
								{
									var counter = Stats.GetIfExists(message.StatOverride.Name);
									if (counter != null)
										counter.ValueOverride = message.StatOverride.Value;
								}
							}
							if (message.VarOverride != null)
							{
								if (!Options.HasFlag(MaintFaceOptions.EnableVariableOverride))
								{
									Trace.WriteLine(nameof(MaintFace) + ": Variable overrides are disabled.");
								}
								else
								{
									TraceNode traceNode;
									if (_nodes.TryGetValue(message.VarOverride.NodeName, out traceNode))
										traceNode.SetOverride(
											message.VarOverride.RootIndex,
											message.VarOverride.VarIndex,
											message.VarOverride.Value);
								}
							}
							if (message.Script != null)
							{
								if (!Options.HasFlag(MaintFaceOptions.EnableCodeInjection))
								{
									Trace.WriteLine(nameof(MaintFace) + ": Code injection is disabled.");
								}
								else
								{
									TraceNode traceNode;
									if (_nodes.TryGetValue(message.Script.NodeName, out traceNode))
										traceNode.SetScript(message.Script.Source);
								}
							}
							if (message.DumpSourceName != null)
							{
								TraceNode traceNode;
								if (_nodes.TryGetValue(message.DumpSourceName, out traceNode))
								{
									ServerSourceMessage response = new ServerSourceMessage();
									response.MessageId = message.MessageId;
									response.Source = traceNode.ScriptSource;
									var serializedResult = serializer.Serialize(response);
									var buffer = Encoding.UTF8.GetBytes(serializedResult);
									var segment = new ArraySegment<byte>(buffer);
									if (webSocketInfo.Socket.State != WebSocketState.Open)
										return;

									await webSocketInfo.SendAsync(segment);
								}
							}
							if (message.TrafficCommand != null)
							{
								if (message.TrafficCommand == "Pause")
									webSocketInfo.PausedFlowGraph = true;
								else if (message.TrafficCommand == "Continue")
									webSocketInfo.PausedFlowGraph = false;
							}
						}
					}
					catch (Exception ex)
					{
						Trace.WriteLine(nameof(MaintFace) + " Error: " + nameof(CommandListener) + ": " + ex.Message);

						// Avoid spinning too fast
						Thread.Sleep(500);
					}
				}
			}
			finally
			{
				Interlocked.Decrement(ref _connectedCount);
			}
		}
示例#3
0
文件: Server.cs 项目: bmdub/MaintFace
		private static async void HandleWebSocketRequest(HttpListenerContext hc)
		{
			HttpListenerWebSocketContext connectionCtx;
			try
			{
				connectionCtx = await hc.AcceptWebSocketAsync(null);

				var webSocketInfo = new WebSocketInfo(connectionCtx.WebSocket);
				_sockets[webSocketInfo.Socket] = webSocketInfo;
				CommandListener(webSocketInfo);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(nameof(MaintFace) + " Error: " + nameof(HandleWebSocketRequest) + ": " + ex.Message);

				return;
			}
		}
示例#4
0
 object ProcessJoinGroup(IHttpServer server, IHttpRequest req, HttpResponseHeader res)
 {
     WebSocketInfo wi = new WebSocketInfo (req, res, JoinWebSocketHandler, new JoinInfo());
     return wi;
 }
示例#5
0
 object ProcessCreateGroup(IHttpServer server, IHttpRequest req, HttpResponseHeader res)
 {
     WebSocketInfo wi = new WebSocketInfo (req, res, GroupOwnerWebSocketHandler, new ALMInfo());
     return wi;
 }
示例#6
0
 object ProcessCandidatePeer(IHttpServer server, IHttpRequest req, HttpResponseHeader res)
 {
     WebSocketInfo wi = new WebSocketInfo (req, res, CandidateWebSocketHandler, new CandidateInfo());
     return wi;
 }