通过WebSocket发送base64字符串

eulz3vhy  于 2022-12-26  发布在  其他
关注(0)|答案(1)|浏览(298)

我使用的是ZKT Ecco ZK4500指纹设备,硬件本身不提供任何Web API,但我需要将用户的指纹发送到ReactJS客户端Web应用程序的服务器。我在c#windows窗体中编写了一个小实用程序,它与硬件SDK交互并获取指纹。然后我将此指纹转换为base64,并使用WebSocket将其发送到ReactJS应用程序(http://sta.github.io/websocket-sharp/ library for C#)。但问题是,每次扫描这个用户的指纹时,我都需要发送它。为此,SDK有一个zkFprint_OnImageReceived方法。**如何让C#中的websocket服务器在这个方法中向我的ReactJS客户端发送指纹?**websocket服务器和客户端之间的连接已经建立。但是我没有' I don "我不知道如何将base64作为参数传递给客户端。感谢您抽出时间。

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");**
            
        }
    }
}

我试着这么做:

    • 在窗体的主类中声明了私有变量**
private string _fp;
    • 创建了私有静态方法**
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);
            
        }
    }
}

结果,我成功地从c#服务器向JS客户端发送了一个指纹,但是出现了另一个问题,现在只有当用户在websocket客户端连接到服务器之前扫描指纹时才发送指纹,并且无论新扫描如何,都将发送之前收到的第一个指纹。

rt4zxlrg

rt4zxlrg1#

将form1_load设置为如下所示

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

StartServer()更改如下

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);

    }

在EchoOnConnect类中,清除所有不必要的

public class EchoOnConnect : WebSocketBehavior
{

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

}

相关问题