DBILITY

C# websoket-sharp 본문

reference

C# websoket-sharp

DBILITY 2021. 5. 9. 18:47
반응형

PingmanTools/websocket-sharp: A C# implementation of the WebSocket protocol client and server (github.com)

 

PingmanTools/websocket-sharp

A C# implementation of the WebSocket protocol client and server - PingmanTools/websocket-sharp

github.com

서버는 spring websocket으로 제작되어 있다.

중요한 것은 소켓접속경로의 마지막이었다...ㅎㅎ

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CsharpTrayAppWebsocketComm.Properties;
using System.Diagnostics;
using System.Threading;
using WebSocketSharp;
using Newtonsoft;
using Newtonsoft.Json.Linq;

namespace CsharpTrayAppWebsocketComm
{
    public partial class Form1 : Form
    {
        private WebSocket webSocket;
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            notifyIcon1.Text = "메시지";
            //noti.Icon = Resources.tray;
            notifyIcon1.MouseDoubleClick += Noti_DoubleMouseClick;
            notifyIcon1.ContextMenuStrip = new ContextMenuStrip();
            notifyIcon1.ContextMenuStrip.Items.Add("열기", Resources.open, Open_Clicked);
            notifyIcon1.ContextMenuStrip.Items.Add("도움말", Resources.help, Help_Clicked);
            notifyIcon1.ContextMenuStrip.Items.Add("종료", Resources.close, CLose_Clicked);
            notifyIcon1.Visible = true;
            
        }

        private void wsConnect()
        {
            try
            {
                webSocket = new WebSocket("ws://portal.dbility.com/sockdata/websocket");
                //webSocket.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
                //webSocket.Origin = "https://portal.dbility.com";  
                webSocket.Compression = CompressionMethod.Deflate;
                webSocket.OnOpen += ServerConnect;
                webSocket.OnClose += ServerDisconnect;
                webSocket.OnMessage += ServerMessage;
                webSocket.OnError += ServerError;
                webSocket.EnableRedirection = true;                
                webSocket.ConnectAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw;
            }
        }

        private void wsClose()
        {
            try
            {
                webSocket.CloseAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw;
            }
        }

        private void ServerError(object sender, ErrorEventArgs e)
        {
            //Debug.WriteLine(e.Message+":"+e.Exception.ToString());
            addMessage("Error --> " + e.Message + ":" + e.Exception.ToString());
        }

        private void ServerMessage(object sender, MessageEventArgs e)
        {
            //Debug.WriteLine(e.Data);
            if (e.IsText)
            {
                var data = JObject.Parse(e.Data);
                if (data["message"].ToString() != "sched")
                {
                    addMessage(data["message"].ToString());
                }
            }
        }

        private void ServerConnect(object sender, EventArgs e)
        {           
            //Debug.WriteLine("Connect --> " + webSocket.ReadyState.ToString());
            addMessage("Connect --> " + webSocket.ReadyState.ToString());
        }

        private void ServerDisconnect(object sender, CloseEventArgs e)
        {
            //Debug.WriteLine("Disconnect --> " + e.Reason + ":"+ e.Code + ":"+ webSocket.ReadyState.ToString());                                  
            addMessage("Disconnect --> " + e.Reason + ":" + e.Code + ":" + e.WasClean);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Visible = false;

            wsConnect();

        }

        private void CLose_Clicked(object sender, EventArgs e)
        {
            if (MessageBox.Show("종료 메뉴 클릭했다. 종료할거니?", "트레이APP", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //Application.Exit(); // message queue에

                //Application.ExitThread();
                //Environment.Exit(0);
                wsClose();
                Process.GetCurrentProcess().Kill();
            }
        }

        private void Help_Clicked(object sender, EventArgs e)
        {
            MessageBox.Show("도움말 메뉴 클릭", "트레이APP");
        }

        private void Open_Clicked(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.Visible = true;
        }

        private void Noti_DoubleMouseClick(object sender, MouseEventArgs e)
        {
                        
            if (this.WindowState == FormWindowState.Minimized)
            {                
                this.WindowState = FormWindowState.Normal;                
                this.ShowInTaskbar = true;
                this.Visible = true;                
            } else
            {                
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;
                this.Visible = false;
            }
            this.Activate();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Visible = false;
        }

        private void addMessage(String message)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate {
                    try
                    {
                        Label l = new Label
                        {
                            TextAlign = ContentAlignment.MiddleLeft,
                            Text = message,
                            Dock = DockStyle.Fill,
                            AutoSize = false
                        };
                        Panel p = new Panel();
                        p.Height = l.Height+2;
                        p.BorderStyle = BorderStyle.FixedSingle;
                        p.Dock = DockStyle.Top;
                        p.Controls.Add(l);
                
                        tableLayoutPanel1.Controls.Add(p);
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }));
            }
        }

    }
}

반응형

'reference' 카테고리의 다른 글

get css rotate degree  (0) 2021.05.19
devdic 개발자 사전  (0) 2021.05.18
favicon generator  (0) 2021.05.08
indexeddb  (0) 2021.04.30
https://jwt.io/ json web token  (0) 2021.04.09
Comments