using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using System; namespace ElectronNET.API { /// /// Communicate asynchronously from the main process to renderer processes. /// public sealed class IpcMain { private static IpcMain _ipcMain; internal IpcMain() { } internal static IpcMain Instance { get { if(_ipcMain == null) { _ipcMain = new IpcMain(); } return _ipcMain; } } /// /// Listens to channel, when a new message arrives listener would be called with /// listener(event, args...). /// /// Channelname. /// Callback Method. public void On(string channel, Action listener) { BridgeConnector.Socket.Emit("registerIpcMainChannel", channel); BridgeConnector.Socket.On(channel, listener); } /// /// Adds a one time listener method for the event. This listener is invoked only /// the next time a message is sent to channel, after which it is removed. /// /// Channelname. /// Callback Method. public void Once(string channel, Action listener) { BridgeConnector.Socket.Emit("registerOnceIpcMainChannel", channel); BridgeConnector.Socket.On(channel, listener); } /// /// Removes listeners of the specified channel. /// /// Channelname. public void RemoveAllListeners(string channel) { BridgeConnector.Socket.Emit("removeAllListenersIpcMainChannel", channel); } /// /// Send a message to the renderer process asynchronously via channel, you can also send /// arbitrary arguments. Arguments will be serialized in JSON internally and hence /// no functions or prototype chain will be included. The renderer process handles it by /// listening for channel with ipcRenderer module. /// /// BrowserWindow with channel. /// Channelname. /// Arguments data. public void Send(BrowserWindow browserWindow, string channel, params object[] data) { BridgeConnector.Socket.Emit("sendToIpcRenderer", JObject.FromObject(browserWindow, _jsonSerializer), channel, data); } private JsonSerializer _jsonSerializer = new JsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }; } }