public int setImmediate(JSCallback callback) { if (callback == null) { return(-1); } string eventName = "setImmediate"; CallbackItem item = null; item = API.client.Callbacks.Add(API.id, eventName, callback); string script = ScriptBuilder.Build( ScriptBuilder.Script( "var callback = () => {{", "{0};", "this.emit('__event',{1},{2},{3});", "}};", "var id = {4};", "return id;" ), Script.RemoveObject("id"), API.id, eventName.Escape(), item.CallbackId, Script.AddObject("callback") ); int objectId = API._ExecuteBlocking <int>(script); item.ObjectId = objectId; script = ScriptBuilder.Build( ScriptBuilder.Script( "var timer = setImmediate({0});", "return {1};" ), Script.GetObject(objectId), Script.AddObject("timer") ); return(API._ExecuteBlocking <int>(script)); }
public int setInterval(JSCallback callback, int delay) { if (callback == null) { return(-1); } string eventName = "setInterval"; CallbackItem item = null; item = API.client.Callbacks.Add(API.id, eventName, callback); string script = ScriptBuilder.Build( ScriptBuilder.Script( "var callback = () => {{", "this.emit('__event',{0},{1},{2});", "}};", "return {3};" ), API.id, eventName.Escape(), item.CallbackId, Script.AddObject("callback") ); int objectId = API._ExecuteBlocking <int>(script); item.ObjectId = objectId; script = ScriptBuilder.Build( ScriptBuilder.Script( "var timer = setInterval({0},{1});", "return {2};" ), Script.GetObject(objectId), delay, Script.AddObject("timer") ); return(API._ExecuteBlocking <int>(script)); }
protected void _OnEvent(SocketronData data) { JsonObject json = new JsonObject(data.Args); //DebugLog("Return: {0}", data.Arguments[0].GetType()); string eventName = json["name"] as string; object args = json["args"]; //_DebugLog("Return: {0}: {1}", eventName, args); if (eventName == "__event") { Thread thread = Thread.CurrentThread; if (!_Clients.ContainsKey(thread)) { _Clients.Add(thread, this); } object[] list = args as object[]; int instanceId = Convert.ToInt32(list[0]); string eventId = list[1] as string; int callbackId = Convert.ToInt32(list[2]); object[] callbackParams = null; const int baseCount = 3; if (list.Length > baseCount) { callbackParams = new object[list.Length - baseCount]; Array.Copy(list, baseCount, callbackParams, 0, list.Length - baseCount); } /* * Callback callback = null; * * className = "Socketron." + Char.ToUpper(className[0]) + className.Substring(1); * Type type = GetType().Assembly.GetType(className); * if (type == null) { * _Clients.Remove(thread); * return; * } * MethodInfo method = type.GetMethod( * "GetCallbackFromId", * BindingFlags.Static | BindingFlags.Public * ); * if (method == null) { * _Clients.Remove(thread); * return; * } * callback = method.Invoke(null, new[] { (object)callbackId }) as Callback; * callback?.Invoke(callbackParams); * //*/ CallbackItem item = Callbacks.GetItem(instanceId, eventId, callbackId); item?.Callback?.Invoke(callbackParams); _Clients.Remove(thread); /* * switch (className) { * case BrowserWindow.Name: * callback = BrowserWindow.GetCallbackFromId((ushort)callbackId); * callback?.Invoke(callbackParams); * break; * case WebContents.Name: * callback = WebContents.GetCallbackFromId((ushort)callbackId); * callback?.Invoke(callbackParams); * break; * } * //*/ return; } if (args is object[]) { Thread thread = Thread.CurrentThread; if (!_Clients.ContainsKey(thread)) { _Clients.Add(thread, this); } Emit(eventName, args as object[]); _Clients.Remove(thread); return; } Emit(eventName, args); }