public virtual void OnReceived(string data) { try { MessageBag msgBag = Newtonsoft.Json.JsonConvert.DeserializeObject <MessageBag>(data); mCurrentBag = msgBag; if (this.Session == null) { if (msgBag.SessionID.IsNullOrEmpty()) { this.Session = SessionState.GetSession(Guid.NewGuid().ToString()); } else { this.Session = SessionState.GetSession(msgBag.SessionID); if (this.Session == null) { this.Session = SessionState.GetSession(Guid.NewGuid().ToString()); } } } if (msgBag.Action == "init") { handleInit(msgBag); } else if (msgBag.Action == "exit") { mCloseStreamHandler(); } else if (msgBag.Action == "UploadFile") { this.StreamType = RemotingStreamType.Bytes; handleUploadFile(msgBag); } else if (msgBag.Action == "w_heart")//websocket心跳包 { SendClientMessage("1", 2); return; } else if (msgBag.Action == "w_msg")//websocket消息 { RemotingController.MessageReceive(this.Session, msgBag.State); return; } else if (msgBag.MethodName.IsNullOrEmpty() == false) { handleMethodInvoke(msgBag); } else if (msgBag.GroupName.IsNullOrEmpty() == false) { this.GroupName = msgBag.GroupName; if (RemotingController.MessageReceiverConnect(this.Session, this.GroupName) == false) { throw new Exception("服务器拒绝你接收信息"); } KeepAliveHandlers.Add(this); return; } } catch (Exception ex) { throw ex; } }
/// <summary> /// 处理 http://host/controller/method类型的函数 /// </summary> /// <param name="fullname"></param> /// <param name="methodName"></param> internal void handleUrlMethod(string fullname, string methodName) { var pageDefine = checkRemotingName(fullname); MethodInfo methodinfo = pageDefine.Methods.SingleOrDefault(m => string.Equals(m.Name, methodName, StringComparison.CurrentCultureIgnoreCase)); if (methodinfo == null) { throw new Exception($"没有找到方法{methodName},可能因为此方法没有定义[RemotingMethod]"); } if (RemotingContext.Current.Request.Headers.ContainsKey("Cookie")) { var match = System.Text.RegularExpressions.Regex.Match(RemotingContext.Current.Request.Headers["Cookie"], @"WayScriptRemoting\=([\w|\-]+)"); if (match.Length > 0) { this.Session = SessionState.GetSession(match.Groups[1].Value); } } if (this.Session == null) { this.Session = SessionState.GetSession(Guid.NewGuid().ToString()); } var currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType); currentPage.Session = this.Session; currentPage.RequestHeaders = new RemotingController.RequestHeaderCollection(_GetHeaderValueHandler); RemotingContext.Current.Controller = currentPage; currentPage.onLoad(); try { RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)methodinfo.GetCustomAttribute(typeof(RemotingMethodAttribute)); var pInfos = methodinfo.GetParameters(); object[] parameters = new object[pInfos.Length]; for (int i = 0; i < parameters.Length; i++) { Type pType = pInfos[i].ParameterType; string name = pInfos[i].Name; string value = null; if (RemotingContext.Current.Request.Query.ContainsKey(name)) { value = RemotingContext.Current.Request.Query[name]; } else if (RemotingContext.Current.Request.Form.ContainsKey(name)) { value = RemotingContext.Current.Request.Form[name]; } if (value == null) { continue; } try { if (pType.GetTypeInfo().IsEnum || (pType.GetTypeInfo().IsGenericType&& pType.GetGenericTypeDefinition() == typeof(Nullable <>) && pType.GetGenericArguments()[0].GetTypeInfo().IsEnum)) { Type enumType = pType; if (pType.GetTypeInfo().IsGenericType) { enumType = pType.GetGenericArguments()[0]; } parameters[i] = Enum.Parse(enumType, value); } else if (pType == typeof(string)) { parameters[i] = value; } else { parameters[i] = Convert.ChangeType(value, pType); } } catch { } } object result = null; result = currentPage._OnInvokeMethod(methodinfo, parameters); RemotingContext.Current.Response.Headers["Set-Cookie"] = $"WayScriptRemoting={this.Session.SessionID};path=/"; if (result is FileContent) { ((FileContent)result).Output(); } else if (result is string) { RemotingContext.Current.Response.Write(result.ToSafeString()); } else if (result != null) { RemotingContext.Current.Response.Write(result.ToJsonString()); } else { RemotingContext.Current.Response.Write(new byte[0]); } } catch { throw; } finally { currentPage.unLoad(); } }