I use SignalR to print a document from a web site through a printer connected to the network. When I test it on a pc, it works fine. The problem is when there is more than one pc connected, since it prints as many copies as the number of computers connected using SignalR.
This is the html code:
$(document).ready(function () {
var myHub = $.connection.myHub;
var direccionWeb = $("#direccionWeb").val().toString();
$.connection.hub.url = direccionWeb + "/signalr";
$.connection.hub.start().done(function () {
myHub.server.broadcastMessageToPrinterReportTicket(global_venta, global_mesa, nombregarzon, idgrupo, grupo);
}).fail(function (error) {
console.error(error);
});
});
This is the HUB file:
public void BroadcastMessageToPrinterReportTicket(String global_venta, String global_mesa, String nombregarzon, String idgrupo, String grupo)
{
string nombreImpresora = grupo;
//resto de código para obtener datos.......
String nombreRDLC = "report_ticket.rdlc";
String nombreDataSet = "DataSet_ticket";
Clients.All.imprimeReport(nombreImpresora, parametros, dt, nombreRDLC, nombreDataSet);
}
This is the windows form:
public partial class Form1 : Form
{
HubConnection hubConnection;
IHubProxy hubProxy;
//Install-Package HtmlRenderer.PdfSharp -Version 1.5.0.6
public Form1()
{
InitializeComponent();
IncializaSignalR();
hubConnection.Closed += IncializaSignalR;
hubConnection.Start().Wait();
}
private void IncializaSignalR()
{
String direccionServer = "https://mipaginaweb.cl";
hubConnection = new HubConnection(direccionServer + "/signalr/hubs");
hubProxy = hubConnection.CreateHubProxy("MyHub");
CheckForIllegalCrossThreadCalls = false;
hubProxy.On<String, ReportParameter[], DataTable, String, String>("imprimeReport", (nombreImpresora, parametros, dt, nombreRDLC, nombreDataSet) =>
{
//Instrucción de código para imprimir con reportviewer
}
});
}
When checking the code, I suspect that the cause of the problem could be the "All" statement:
Clients.All.imprimeReport(.......
Because apparently, it sends the request to all connected users. However, the instruction should go only from the client's PC who made the request to print, as intended.