-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to deal the .NET's event with the nodejs event's handler. #330
Comments
I tried many times as read the instruction: |
delegate tag can not submit above "Func<object, Task>" |
the erro is cannot wait System.Func<object,System. |
I and i saw .net can handle node events, but node can not handle .net events, that's not fair. |
@gentlecolder The functions Edge.js can marshal from JavaScript to C# must have the following signature:
These functions will then have the shape of the following delegate on the C# side:
If you plan to hook up JavaScript function as a .NET event handler, you will need to provide a wrapper event handler implementation around the marshaled |
The debug complied error is: then i changed the Func<object,Task > to Func<object,Task<System.Collections.Generic.IList > the debug give me the message is "Invalid expression term". |
it seamed that the node function can not changed to Eventhandler implicitly, why? doest that means Eventhandler is not a function in C#. I am not good at C# |
Edge.js does not marshal arbitrary functions between JavaScript and .NET, the functions must be of appropriate signature. It means that in most cases you will need to write an adapter code or class that changes the intended signature of the function: https://github.com/tjanczuk/edge#how-to-integrate-c-code-into-nodejs-code. In your case, I believe you need to write a wrapper around *EventHandler type expected in your C# code and the |
above code i have showed wrapped still cannot pass complie |
@tjanczuk Could you please provide a simple example of EdgeJS code (Node.JS) code handling .NET events properly? I want to start an ID Card Reader application (for 3M CR100 SwipeReader) and I want my (edgejs + nodejs) code to listen to the DocumentReadEvent and DocumentReadErrorEvent from my C# code. I hope you see this asap. Please reply to this comment just to see you have seen it and if you can apply some action to this issue commented here. With all due respect, |
@gentlecolder have you been able to assign a javascript event handler to a C# event? |
@danielmihai Are you writing .NET application or a Node.js application (i.e. is Node hosted in CLR or vice versa)? |
Have a look at https://github.com/tjanczuk/edge/blob/master/samples/209_websocket.js, specifically at how the |
@tjanczuk I want to send a function from JavaScript that acts as an event handler to In c# on Invoke, I'll assign the EventHandler to the Event: public EventHandler DocumentReadEvent; .... Invoke (dynamic input) { this.swipeReader = new SwipeReader(); this.DocumentReadEvent += input.handleCSeventEventHandler; } The scenario I'm in : I've written a library that initializes a 3M private void ProcessEventThreadHelper(MMM.Readers.FullPage.EventCode private void ProcessEvent(MMM.Readers.FullPage.EventCode aEventCode)
In InitializeSwipeAPI i have
handler callback
files. You can
sent back in a
return 1; How can I keep the DLL alive in nodejs so that it continually runs and Kind Regards, On 21 ian. 2016, at 22:27, Tomasz Janczuk [email protected] wrote: Have a look at — |
This line of code will require some extra work:
The |
Hi @tjanczuk , I managed to get it working by integrating this example https://github.com/tjanczuk/edge/blob/master/samples/209_websocket.js and sending the function to the DLL. public abstract class NetWebSocket
{
private Func<object, Task<object>> SendImpl { get; set; }
protected NetWebSocket(Func<object, Task<object>> sendImpl)
{
this.SendImpl = sendImpl;
}
protected abstract Task ReceiveAsync(string message);
public Func<object, Task<object>> ReceiveImpl
{
get
{
return async (input) =>
{
Console.Out.WriteLine(input);
await this.ReceiveAsync((string) input);
return Task.FromResult<object>(null);
};
}
}
protected async Task SendAsync(string message)
{
await this.SendImpl(message);
return;
}
}
public class MyNetWebSocketImpl : NetWebSocket
{
public CHello module;
private string JSONCodelineDataRepr = "not set";
public MyNetWebSocketImpl(Func<object, Task<object>> sendImpl) : base(sendImpl)
{
// do other stuff after calling the super class constructor
module = new CHello();
module.DocumentReadEvent += this.DocumentReadEventHandler;
module.DocumentReadErrorEvent += this.DocumentReadErrorEventHandler;
// uncomment after the websocket communication works
module.Start();
}
protected override async Task ReceiveAsync(string message)
{
// not really needed because only the NodeJS Server listens to C# .NET Server messages
Console.WriteLine(message);
if (message.Equals("shutdown"))
{
module.Close();
}
// not necessary (can comment the send function call)
// if I eventually receive a message, respond with the JSON representation of the Patient ID Card
await this.SendAsync("I received a message from you, but I'll ignore it and send you the Patient" +
" ID Card Data instead.. I'm a fish, so start phishing! PersonData = " +
JSONCodelineDataRepr);
return;
}
private async void DocumentReadEventHandler(string args)
{
this.JSONCodelineDataRepr = args;
await this.SendAsync(args);
}
private async void DocumentReadErrorEventHandler(string args)
{
await this.SendAsync(args);
}
}
public class Startup
{
public static MyNetWebSocketImpl ws;
public async Task<object> Invoke(Func<object, Task<object>> sendImpl)
{
ws = new MyNetWebSocketImpl(sendImpl);
return ws.ReceiveImpl;
}
} (function(e,d,g,e){
var edge = require('edge'),
http = require('http'),
WebSocketServer = require('ws').Server,
swipe = edge.func('./dlls/ActiveXCOM.dll');
var server = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end((
function () { /*
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id='jsonOutput'>
</div>
<script>
var ws = new WebSocket('ws://' + window.document.location.host);
ws.onmessage = function (event) {
console.log(event.data);
var div = document.getElementById('jsonOutput');
div.innerHTML = event.data;
}
ws.onopen = function (event) {
// send something to the server
ws.send('I am the client from the browser calling you, the NodeJS WebSocketServer!');
}
ws.onclose = function (event) {
alert('websocket closing');
}
window.onbeforeunload = function myonbeforeUnload() {
return "Are you sure?";
}
window.onunload = function myonunload() {
confirm('Are you really sure?');
ws.close();
return "Are you really sure?";
}
</script>
</body>
</html>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1]);
});
var wss = new WebSocketServer({server: server});
wss.on('connection', function(ws) {
var sendImpl = function (message, callback) {
console.log(message);
ws.send(message);
callback();
};
var receiveHandler = swipe(sendImpl, true);
ws.on('message', function (message) {
receiveHandler(message);
});
ws.on('close', function close(){
console.log('****************************The client disconnected!');
receiveHandler('shutdown');
delete receiveHandler;
});
});
server.listen(process.env.PORT || 8080);
module.exports = this;
})(); This kind of does what it should. Thank you very very much! ^:)^ There are no words to thank you enough! Would you like to include this example of handling C# events in Javascript (Node) to the samples folder? I would be glad to offer the full implementation to have a mini contribution to EdgeJS (Samples only, for now, who knows what might happen in the future), if you agree :D. |
@tjanczuk Another question popped into my mind, if it's possible to convert this into a client-side javascript module and have things packed like nw.js does ? What do you think? Thank you again for your support, examples and wonderful library! ^:)^ 🙇 |
@tjanczuk Any news? |
Hi @danielmihai , can I ask, if you solved this your situation? I deal with a similar problem as you, only I need to handle C# events from RFID reader. |
If you keep the same pattern as in the example I've written, it doesn't matter what you expose. |
@danielmihai , Hi, how did you get your swipe reader working. I'm working with my fingerprint scanner but no luck in listening for events. currentReader.On_Captured += new `Reader.CaptureCallback(OnCaptured); ` |
I would first make a test program in c#, then adapt it to work for edge, then write the DLL I really need for my app
Daniel M. Colceag
… On 20 Apr 2017, at 13:26, Daryll Tee ***@***.***> wrote:
@danielmihai , Hi, how did you get your swipe reader working. I'm working with my fingerprint scanner but no luck in listening for events.
Thanks for you responce.
// Activate capture handler currentReader.On_Captured += newReader.CaptureCallback(OnCaptured);``
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
the problem code is bellow:
/the edge code bellow: edged.js/
var stractData = edge.func(function () {/*
*/});
module.exports.stractData=stractData;
/the nodejs code bellow:/
var databox=require('edged.js');
var util = require('util');
var input = {
DataChangedHandler: function (object sender, IList e) {
foreach (DataMonitorValueChangedArgs monitorValue in e)
{
};
databox.stractData(input);
is there any good solution ?
The text was updated successfully, but these errors were encountered: