//public static object ScriptExec(MyWebSocketSession session, string scrpitTxt) //{ // //if (runtimeHost._isDesign) // //{ // // runtimeHost.printout(scrpitTxt); // //} // //创建一个IpyRunTime,需要2-3秒时间。建议进入全局时加载,此为演示 // var engine = CreateScriptEngine();// IronPython.Hosting.Python.CreateEngine(); // var code = engine.CreateScriptSourceFromString(scrpitTxt); // //设置参数; // ScriptScope scope = engine.CreateScope(); // scope.SetVariable("sessionHost", session); // var actual = code.Execute<object>(scope); // return actual; //} public static object ScriptExec(MyWebSocketSession session, string scrpitTxt) { //if (runtimeHost._isDesign) //{ // runtimeHost.printout(scrpitTxt); //} try { //创建一个IpyRunTime,需要2-3秒时间。建议进入全局时加载,此为演示 var engine = CreateScriptEngine();// IronPython.Hosting.Python.CreateEngine(); var code = engine.CreateScriptSourceFromString(scrpitTxt); //设置参数; ScriptScope scope = engine.CreateScope(); scope.SetVariable("sessionHost", session); var actual = code.Execute <object>(scope); return(actual); } catch (Exception exp) { MessageBox.Show(exp.Message); return(null); } }
public void CloseClient(WebSocketSession session, SuperSocket.SocketBase.CloseReason reason) { if (this.InvokeRequired) { this.BeginInvoke( new CloseClient_Delegate(CloseClient), new Object[] { session, reason }); return; } try { string str = ""; string sessionid = session.SessionID; MyWebSocketSession oldSessoin = sessionList.Find(delegate(MyWebSocketSession temp) { return(temp.sessionid == session.SessionID); }); if (oldSessoin != null) { sessionList.Remove(oldSessoin); } //this.dataGridView1.DataSource = new BindingList<MyWebSocketSession>(sessionList); str += session.SessionID + "-离开," + reason.ToString(); //增加连接记录; OnAddLogEvent("连接", str); } catch (Exception exp) { //捕捉错误; //this.CatchException(exp); } }
/// <summary> /// 有新的连接; /// </summary> /// <param name="session"></param> public void AddClient(WebSocketSession session) { if (this.InvokeRequired) { this.BeginInvoke( new AddClient_Delegate(AddClient), new Object[] { session }); return; } try { //判断连接是否已经存在; bool bsend = false; MyWebSocketSession oldSessoin = sessionList.Find(delegate(MyWebSocketSession temp) { return(temp.sessionid == session.SessionID); }); if (oldSessoin == null) { //加入; MyWebSocketSession newSession = new MyWebSocketSession(session); newSession.userid = ""; sessionList.Add(newSession); //循环处理脚本; newSession.loopScripTxt = this.pythonLoopScriptTxt; //DealSession(newSession); } //IList<T> list = new List<T>(); //DataGridView.DataSource = list;//DataGridView的行不能添加删除 //DataGridView.DataSource = new BindingList<T>(list);//DataGridView的行可以添加删除(只有允许添加行、删除行) //this.dataGridView1.DataSource = new BindingList<MyWebSocketSession>(sessionList); string str = ""; str += session.SessionID + "-连接," + session.SocketSession.Client.RemoteEndPoint.ToString(); //DataRow dr = dtSession.NewRow(); //dr["sessionid"] = session.SessionID; //dr["ipinfo"] = session.SocketSession.Client.RemoteEndPoint.ToString(); //dr["userid"] = ""; //dtSession.Rows.Add(dr); //增加连接记录; if (OnAddLogEvent != null) { OnAddLogEvent("连接", str); } } catch (Exception exp) { //捕捉错误; //this.CatchException(exp); } }
private void DealMsg(WebSocketSession session, string msg) { try { if (OnAddLogEvent != null) { OnAddLogEvent("连接", msg); } //MessageBox.Show(hashtable.Count.ToString()); MyWebSocketSession ReqSessoin = sessionList.Find(delegate(MyWebSocketSession temp) { return(temp.sessionid == session.SessionID); }); if (ReqSessoin != null) { ReqSessoin.LastMsg = msg; //执行脚本; WebSocketIronPythonManager.ScriptExec(ReqSessoin, this.pythonScriptTxt); } } catch (Exception ex) { } }