1

我想将字节流从 IHubProxy 传输到另一个客户端。我将字节流作为字符串进行传输。我能够毫无问题地传输小文本。但是当我传输大字符串(这是一个转换为字节流和字符串的图像)时,我无法传输字符串。如何传输大字符串?

        string username = Console.ReadLine();

        var connection = new HubConnection("http://localhost:51428/");
        IHubProxy myHub = connection.CreateHubProxy("ScannerHub"); //This should be same as Hub Name

        //Establishing the connection
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

                //After connection is established. Register the printer client with client information
                myHub.Invoke("RegisterClient", username, connection.ConnectionId, "Printer").ContinueWith(task2 =>
                {
                    if (task2.IsFaulted)
                    {
                        Console.WriteLine("An error occurred during the method call {0}", task2.Exception.GetBaseException());
                    }
                    else
                    {
                        Console.WriteLine("Successfully Registred");
                    }
                });

                //In the client (here it is Printer Client) there is a method named getScanRequest
                //This method retrieves the scan request and processes it accordingly.
                //What is done here is that, in the HubProxy we listen to the calls on current clients 
                //getScanRequest been hit. If hit. Execute that method.

                //Use a Func when you want to return a value
                //http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate

                //Func<string, byte[]> scanFunction;
                //scanFunction = Scan;

                //Action<string> printFunction;
                //printFunction = Scan;

                //myHub.On("getScanRequest", data => Console.WriteLine("Got Print Request " + data));
                //Register on a server side method with a callback function
                //myHub.On("getScanRequest", printFunction);
                //myHub.On("getScanRequest", data => Console.WriteLine("got print request from "+data));

                myHub.On<string>("getScanRequest", (clientUrl) =>
                    {
                        string scanByteStream = Scan(clientUrl);
                        try
                        {
                            myHub.Invoke("GetValueFromScanner", scanByteStream);
                        }
                        catch (Exception ex)
                        {
                            var j = 0;
                        }
                    });
            }
4

0 回答 0