C#

websocket example

c# Server example

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 WebSocketSharp;
using WebSocketSharp.Server;

namespace WindowsFormsApplication3
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        WebSocketServer wssv = null;
        public static Label lab0101;
        public delegate void SetTextCallback01(string text);

        private void button1_Click(object sender, EventArgs e)
        {
            lab0101 = label1;
            wssv = new WebSocketServer("ws://0.0.0.0:3381");
            wssv.AddWebSocketService<Echo1>("/echo");

            wssv.AuthenticationSchemes = WebSocketSharp.Net.AuthenticationSchemes.Basic;
            wssv.Realm = "WebSocket Test josh123456789";
            wssv.UserCredentialsFinder = id => {
                var name = id.Name;

                // Return user name, password, and roles.
                return new WebSocketSharp.Net.NetworkCredential(name, "password");
            };

            wssv.Start();
        }

        public class Echo1 : WebSocketBehavior
        {
            protected override void OnMessage(MessageEventArgs e)
            {
                string arg01 = Context.QueryString["arg01"];
                var str01 = Context.IsAuthenticated;
                var str02 = Context.Headers["Authorization"];
                var str03 = Context.WebSocket.Credentials;
                var str04 = Context.User.Identity.Name;
                string clinetMessage = e.Data;
                SetText(e.Data);

                Send("Hello Client!");
                
            }
            private void SetText(string text)
            {
                
                if (lab0101.InvokeRequired)
                {
                    SetTextCallback01 d = new SetTextCallback01(SetText);
                    lab0101.Invoke(d, new object[] { text });
                }
                else
                {
                    lab0101.Text = text;
                }
                
            }
        }



    }
}

c# Client example

private void button4_Click(object sender, EventArgs e)
{
    var ws = new WebSocket("ws://app02:password1234@127.0.0.1:3381/echo");
    ws.SetCredentials("app02", "password1234",false);
    ws.OnMessage += Ws_OnMessage;
    ws.Connect();
    ws.Send("BALUS");
}

private void Ws_OnMessage(object sender, MessageEventArgs e)
{
    string st01 = e.Data;
    //throw new NotImplementedException();
}

html5 Client example


<!DOCTYPE HTML>
<html>
   <head>
   <meta charset="utf-8">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               //alert("您的瀏覽器支持 WebSocket!");
               
               // 打开一个 web socket
               var ws = new WebSocket("ws://nameJosh123:password@127.0.0.1:3381/echo?arg01=nobita168168");
               
               ws.onopen = function()
               {
                  // Web Socket 已連接上,使用 send() 方法發送資料
                  ws.send("Hello Server!");
               };
                
               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  //alert("資料已接收..."+received_msg );
                  $("#data").append(received_msg);
               };
                
               ws.onclose = function()
               { 
                  alert("連接已關閉..."); 
               };
            }
            
            else
            {

               alert("您的瀏覽器不支持 WebSocket!");
            }
         }
      </script>
        
   </head>
   <body>
      <div id="sse">
         <a href="javascript:WebSocketTest()">Run WebSocket</a>
      </div>
      <div id="data"></div>
   </body>
</html>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。