Sending base64 string via websocket

332 Views Asked by At

I am using ZKT Ecco ZK4500 fingerprint equipment. The hardware itself does not provide any web api, but I need the user's fingerprint to be sent to the server on my ReactJS client web application. I wrote a small utility in c# windows forms that interacts with the hardware SDK and gets a fingerprint. Then I convert this fingerprint to base64 and send it to the ReactJS application using WebSocket (http://sta.github.io/websocket-sharp/ library for C#). But the problem is that I need to send this user's fingerprint every time it is scanned. To do this, the SDK has a zkFprint_OnImageReceived method. How can I make the websocket server in C# send a fingerprint to my ReactJS client in this method? The connection between the websocket server and the client is established. But I don't understand how to pass base64 as a parameter to send to the client. Thank you for your time.

private void Form1_Load(object sender, EventArgs e)
{
    Controls.Add(ZkFprint);
    InitialAxZkfp();
    StartServer();
}
private void zkFprint_OnImageReceived(object sender, IZKFPEngXEvents_OnImageReceivedEvent e)
{

    Graphics g = fpicture.CreateGraphics();
    Bitmap bmp = new Bitmap(fpicture.Width, fpicture.Height);
    g = Graphics.FromImage(bmp);               
    int dc = g.GetHdc().ToInt32();
    ZkFprint.PrintImageAt(dc, 0, 0, bmp.Width, bmp.Height);
    g.Dispose();
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Jpeg);
    byte[] byteImage = ms.ToArray();
    **string fp = Convert.ToBase64String(byteImage);**
}
public void StartServer()
{
    WebSocketServer srv = new WebSocketServer("ws://127.0.0.1:7650");

    srv.AddWebSocketService<EchoOnConnect>("/");
    srv.Start();

    Console.WriteLine("Server started on {0}:{1}", srv.Address, srv.Port);
    srv.Stop();
}
public class EchoOnConnect : WebSocketBehavior
{
    protected override void OnOpen()
    {
        Console.WriteLine("Session started.");
        Console.WriteLine("Send fingerprint");

        while (true)
        {
            Thread.Sleep(2000);
           ** Send("Here you need to somehow send a fingerprint");**
            
        }
    }
}

I tried to do so:

Declared a private variable in the main class of the form

private string _fp;

Created a private static method

private static string setFingerprint(string fingerprint, ref string _fp)
{
    _fp = fingerprint;
    return _fp;
}
public void StartServer()
{
    string _fp = null;

    WebSocketServer srv = new WebSocketServer("ws://127.0.0.1:7890");

   ** srv.AddWebSocketService<EchoOnConnect>("/", () => new EchoOnConnect(_fp));**

    srv.Start();

    Console.WriteLine("Server started on {0}:{1}", srv.Address, srv.Port);

    wssv.Stop();
}
private void zkFprint_OnImageReceived(object sender, IZKFPEngXEvents_OnImageReceivedEvent e)
{
    Graphics g = fpicture.CreateGraphics();
    Bitmap bmp = new Bitmap(fpicture.Width, fpicture.Height);
    g = Graphics.FromImage(bmp);               
    int dc = g.GetHdc().ToInt32();
    ZkFprint.PrintImageAt(dc, 0, 0, bmp.Width, bmp.Height);
    g.Dispose();
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Jpeg);
    byte[] byteImage = ms.ToArray();
    string fp = Convert.ToBase64String(byteImage);
    **setFingerprint(fp, ref _fp);**
}
public class EchoOnConnect : WebSocketBehavior
{
    private string _fingerprint;

    public EchoOnConnect() : this(null)
    {
    }

    public EchoOnConnect(string fingerprint)
    {
        _fingerprint = fingerprint ?? String.Empty;
    }


    protected override void OnOpen()
    {
        Console.WriteLine("Session started");
        Console.WriteLine("I'm send fingerprint" + _fingerprint);

        while (true)
        {
            Thread.Sleep(2000);
            Send(_fingerprint);
            
        }
    }
}

As a result, I managed to send a fingerprint from the c # server to the JS client, but another problem appeared. Now the fingerprint is only sent when the user scans it before the websocket client connects to the server and regardless of new scans, the very first fingerprint that was received before is sent.

1

There are 1 best solutions below

0
On BEST ANSWER

Make your form1_load look like this

        private void Form1_Load(object sender, EventArgs e)
    {
        Controls.Add(ZkFprint);
        InitialAxZkfp();
        Thread thread = new Thread(StartServer);
        thread.Start();
    }

StartServer() change as follows

        public void StartServer()

    {

        WebSocketServer srv = new WebSocketServer("ws://127.0.0.1:7890");
        srv.AddWebSocketService<EchoOnConnect>("/");
        srv.Start();
        while (true)
        {
            if (fp != null)
            {
                srv.WebSocketServices["/"].Sessions.Broadcast(fp);
                Thread.Sleep(1000);
                Console.WriteLine("Broadcast");
                fp = null;
            }

        }

        Console.WriteLine("Server started on {0}:{1}", srv.Address, srv.Port);

    }

in the EchoOnConnect class, clear all unnecessary

    public class EchoOnConnect : WebSocketBehavior
{

    protected override void OnOpen()
    {
        Console.WriteLine("Session started");
        Console.WriteLine("I'm send fingerprint");
    }

}