예제 #1
0
        private void socketIo()
        {
            var socket = IO.Socket(this.server);

            //Event
            socket.On(Socket.EVENT_CONNECT, () =>
            {
                List <String> printer = getPrinter();
                string defaultPrinter = getDefaultPrinter();

                var data = new
                {
                    ip             = this.ip,
                    name           = this.name,
                    printers       = printer,
                    defaultPrinter = defaultPrinter
                };

                var jsonFormat = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                socket.Emit("recieveUserName", jsonFormat);
                notifyIcon.BalloonTipText = "Connected Server " + server;
                notifyIcon.ShowBalloonTip(1000);
            });

            var that = this;

            socket.On("message", (data) =>
            {
                Invoke(new Action(() =>
                {
                    that.Show();
                    that.WindowState   = FormWindowState.Normal;
                    notifyIcon.Visible = false;

                    var o = (JObject)data;

                    //tboMessage.Text = (string)o.GetValue("message");
                    String message = (string)o.GetValue("message");
                    //message = message.Replace(System.Environment.NewLine, "\n");
                    label1.Text    = message;
                    String strType = (string)o.GetValue("type");
                    if (strType != null)
                    {
                        setType(type[strType]);
                    }
                    //SFCForm.MainWindow fm = new SFCForm.MainWindow();
                    label1.Top = (this.Height - label1.Height) / 2 - 30;
                }));
            });

            socket.On("print", (data) =>
            {
                try
                {
                    Invoke(new Action(() =>
                    {
                        notifyIcon.BalloonTipText = "Client Printed.";
                        notifyIcon.ShowBalloonTip(1000);

                        var o = (JObject)data;
                        //JArray jarray = (JArray)o.GetValue("JOBS");
                        //JToken entry = (JToken)jarray[0];
                        //var jobs = (String)entry[0]["DocumentName"];
                        LocalReport localReport = new LocalReport();

                        foreach (JObject job in (JArray)o.GetValue("JOBS"))
                        {
                            localReport.ReportPath = (String)job.GetValue("FilePath");
                            foreach (JObject doc in (JArray)job.GetValue("DocumentDatas"))
                            {
                                try
                                {
                                    ReportParameter Param = new ReportParameter();
                                    Param.Name            = (String)doc.GetValue("Parameter");
                                    Param.Values.Add((String)doc.GetValue("Value"));
                                    localReport.SetParameters(new ReportParameter[] { Param });
                                }
                                catch (LocalProcessingException e)
                                {
                                    MessageBox.Show(e.InnerException.Message);
                                    break;
                                }
                            }

                            if ((String)job.GetValue("PrintServerID") != (String)job.GetValue("PrinterName"))
                            {
                                localReport.SetPrinterName((String)job.GetValue("PrinterName"));
                                localReport.PrintToPrinter();
                            }
                            else
                            {
                                localReport.PrintToPrinter(); //default printer output
                            }
                        }

                        // LocalReport localReport = new LocalReport();
                        // localReport.ReportPath = Application.StartupPath + "\\FGRECEIVEDBILL_REPORT.rdl";

/*
 *                      foreach (var x in (JObject)o.GetValue("item"))
 *                      {
 *                          ReportParameter Param = new ReportParameter();
 *                          Param.Name = (String)x.Key;
 *                          Param.Values.Add((String)x.Value);
 *                          localReport.SetParameters(new ReportParameter[] { Param });
 *                      }
 *
 *                      localReport.PrintToPrinter((String)o.GetValue("printer"));
 */
                    }));
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            });

            socket.On(Socket.EVENT_ERROR, (error) => {
                // ...

                Invoke(new Action(() =>
                {
                    notifyIcon.BalloonTipText = "Connect Error " + server;
                    notifyIcon.ShowBalloonTip(1000);
                }));
            });
        }