SlideShare ist ein Scribd-Unternehmen logo
1 von 25
ASP.NET CORE 帶您
讀源碼
WebSocket 篇
WebSocket
■ Duplex bi-directional Api for web
server  client
■ RFC 6455
https://tools.ietf.org/html/rfc6455
■ W3C standard:
https://www.w3.org/TR/websockets
/
■ Transport protocol intro:
https://www.youtube.com/watch?v=
9FqjRN4VYUU
■ Js Client & protocol packet format
intro: https://hpbn.co/websocket/
WebSocket
Connect State Transition
1. Connection Upgrade
(Protocol Switch)
2. Data In/Out arbitrarily
(Ping  Pong to detect online)
3. Close connection handshake
Transfer “Frame” format
Read the source?!
Experiment with source!!
(tracing/logging or make it
debuggable)■ Client :
websocket-sharp
https://sta.github.io/websocket-
sharp/
■ Server :
ASP.NET Core v2.1.1 Websocket
https://docs.microsoft.com/en-
us/aspnet/core/fundamentals/webso
ckets?view=aspnetcore-2.1
websocket-sharp
■ Run on .NET Framework 3.5 and above, Mono, Unity3D
■ A single DLL provide websocket client/server functionality, comply with RFC 6455
■ HTTPS encrypt/decrypt algorithm is via Framework functionality
■ MIT license
websocket-sharp
■ Client usage is very simple:
https://github.com/sta/websocket-
sharp#websocket-client
■ Has event hook let client handler websocket event
easily: http://bit.ly/2ztELl9
■ Mimic Js Websocket API:
https://developer.mozilla.org/en-
US/docs/Web/API/WebSocket
■ Entry Point: the “WebSocket” class: http://bit.ly/2L76Xf2
■ WebSocket Connect() Implementation:
– Connect() API : http://bit.ly/2KVjLsf
 doHandshake() : http://bit.ly/2J9ySJn
 setClientStream() : http://bit.ly/2zsdLCN
 createHandshakeRequest() : http://bit.ly/2KKAkHO
 sendHttpRequest() : http://bit.ly/2JeiYgX
 checkHandshakeResponse() : http://bit.ly/2zsim7O
=> Create “NetworkStream (http://bit.ly/2L3q5xE)” for afterward R/W operation
■ WebSocket Send() Implementation:
– Send() API : http://bit.ly/2LaYY0I
 send(Opcode opcode, Stream stream, bool compressed) : http://bit.ly/2N31aYq
 send(Fin fin, Opcode opcode, byte[] data, bool compressed) : http://bit.ly/2KPszQQ
 sendBytes(byte[] bytes) : http://bit.ly/2zoQCkx
=> Wrap input into a lot of “WebSocketFrame (http://bit.ly/2N5zfHh)” then write to stream
■ WebSocket OnMessage event Implementation:
– EventHandler<MessageEventArgs> OnMessage :
■ There’re two place emit the event:
– messagec(MessageEventArgs e) : http://bit.ly/2L7etGO
■ This is binding to Action<> _message field, which is invoked On:
– open() : http://bit.ly/2uaCGpl
■ Being called at Connect() API : http://bit.ly/2L65rgE
– message() : http://bit.ly/2KMUt00
– startReceiving() : http://bit.ly/2NEcATw
– open() : http://bit.ly/2zssHAE
=> Get data from internal _messageEventQueue.Dequeue() then invoke the event.
ASP.NET Core Websocket Server
■ The whole implementation across many “Nuget Packges (https://www.nuget.org/packages)”:
– Microsoft.AspNetCore.Websockets
– Microsoft.AspNetCore.Http.Abstractions
– Microsoft.AspNetCore.Http
– Microsoft.AspNetCore.Http.Extensions
– Microsoft.AspNetCore.Http.Features
– Microsoft.AspNetCore.Server.Kestrel
– Microsoft.AspNetCore.Server.Kestrel.Core
– System.Net.WebSockets.WebSocketProtocol
– System.Net.WebSockets
■ It’s impossible to get through whole source code simply by human 👀!!!
■ We have to “experiment it” with debuggable source code or being able to logging detail
information.
Build ASP.Net Core Framework
Package
(make it debuggable)■ Build debuggable ASP.NET Core nuget packages?
1. Clone the official build repo: Universe https://github.com/aspnet/Universe
2. Build it…...since ASP.NET Core module’s source has almost unified folder
structure convention:
■ src: The real source code
■ test: Testing code
■ sample: sample project or test to verify production source
■ build: build configuration files
■ Write a example project to use those debuggable packages then run it, dive into….
Build ASP.Net Core Framework Package
■ To build individual packages:
1. In Windows machine, Install chocolatey, git for windows, Visual Studio 2017
and ASP.NET workload, node.js (for npm):
https://github.com/aspnet/Universe/wiki/Setting-a-machine-up-to-run-Universe
2. Clone Universe repo with correct tag ( -b 2.1.1), be sure to use --recursive to
get associated git submodule repo in modules folder.
3. Run build.cmd (build.sh) on top folder to let ASP.NET Core Buildtools setup
correct config files on first time, even if not all module can successful build.
4. Make sure the residue build process are all killed.
5. Switch to the module folder you want to build, delete anything in
artifactsbuild subfolder, then use:
build.cmd /p:CompileOnly=true /p:SkipTests=true
To build those Nuget packages of the module, Resulting files will reside in
module folder's artifactsbuild folder, and by default, they are debuggable.
Build ASP.Net Core Framework
Package
(make it debuggable)■ Use “find –iname“ or “dir /s” to find Nuget packages real source location in
Universe repo:
■ Exception:
Microsoft.AspNetCore.Server.Kestrel
Microsoft.AspNetCore.Server.Kestrel.Core
is in modulesKestrelHttpServer folder
■ But the
System.Net.WebSockets.WebSocketProtocol
System.Net.WebSockets
packages are .NET Core runtime’s built-in libraries, that belongs to the
“CoreFx (https://github.com/dotnet/corefx)” repo, not in ASP.NET Core
Time Savior: SourceLink support in
VisualStudio
(https://github.com/dotnet/sourcelink )
■ Visual Studio 2017 v15.7 and above support
SourceLink ( https://docs.microsoft.com/en-
us/visualstudio/releasenotes/vs2017-
relnotes#debug) ,
■ Begin from ASP.NET Core 2.0 it support
SourceLink too:
https://github.com/dotnet/core/issues/897,
but some packages may not ready:
https://github.com/dotnet/buildtools/issues/
1896
■ Most of time it just works 👍
■ Caveat:
– Can not use “GoToDefinition(F12)” if it hasn’t been using debugger dive into it.
– If source code already disappear on GitHub, it cannot work.
– Some too deep function(s) may not work (may be bug?!)
Example experiment project
■ Example source repo:
http://bit.ly/2KO3B4n
■ It use .NET Core SCD deployment
(http://bit.ly/2maxCgj)to let
ASP.NET core runtime use
debuggable Nuget packages we
created.
■ Runs only on Win10-x64
machine.
Some interesting digging result: (1)
■ WebSocket connection setup is implmented in “DefaultWebSocketManager”, its
AccetpWebSocketAsync() is forwarding Websocket upgrade connection work to
WebSocketMiddleware: http://bit.ly/2ukREs1
■ WebSocket SHA-1 encrypt key is hard-coded: http://bit.ly/2N2oTYT
Some interesting digging result: (2)
■ The real “Connection Upgrade” phase is done in
Microsoft.AspNetCore.Server.Kestrel
nuget package’s code, which is Web Server itself:
http://bit.ly/2umVGjz
■ And the WebSocket(http://bit.ly/2KO5uhK) instance is created from websocket
middleware using “HttpResponseStream” as source, the real class is
“ManagedWebSocket”, an internal un-documented Class:
Some interesting digging result: (3)
■ The Actual ReadAsync() & WriteAsync() implementation entry point is extension
methods: http://bit.ly/2uo2ZaL
■ Write data to websocket’s final operations is call HttpUpgradeStream’s
WriteAsync(), which is also located in Web Server itself (the
Microsoft.AspNetCore.Server.Kestrel.Core Nuget package) :
http://bit.ly/2uaKk33
■ Closing websocket implementation is done by ManagedWebSocket class itself:
http://bit.ly/2mbw9pW
WebSocket’s ReadAsync() final operation:
WebSocket’s WriteAsync() final operation:
Conclusion
■ ASP.NET Core use many Abstract class and Interface in its API, and the real
implementation is DefaultOOXX most of the time.
Ex:
WebSocketManager  DefaultWebSocketManager
HttpContext  DefaultHttpContext
■ ASP.NET Core’s Middleware is binding via “Microsoft.AspNetCore.Http.Features”
Nuget’s code.
Resources & Tools:
■ SourceGrpah: https://sourcegraph.com/
■ VSCode & OmniSharp extension:
https://code.visualstudio.com/Docs/languages/csharp
■ MDN:
– Websocket API: https://developer.mozilla.org/en-
US/docs/Web/API/Websockets_API
– Writing WebSocket servers: https://developer.mozilla.org/en-
US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers

Weitere ähnliche Inhalte

Was ist angesagt?

Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
Databricks
 
Hive and Apache Tez: Benchmarked at Yahoo! Scale
Hive and Apache Tez: Benchmarked at Yahoo! ScaleHive and Apache Tez: Benchmarked at Yahoo! Scale
Hive and Apache Tez: Benchmarked at Yahoo! Scale
DataWorks Summit
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
liqiang xu
 

Was ist angesagt? (20)

普通のエンジニアが【ロジバン】やってみた
普通のエンジニアが【ロジバン】やってみた普通のエンジニアが【ロジバン】やってみた
普通のエンジニアが【ロジバン】やってみた
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Hadoop World 2011: Hadoop Troubleshooting 101 - Kate Ting - Cloudera
Hadoop World 2011: Hadoop Troubleshooting 101 - Kate Ting - ClouderaHadoop World 2011: Hadoop Troubleshooting 101 - Kate Ting - Cloudera
Hadoop World 2011: Hadoop Troubleshooting 101 - Kate Ting - Cloudera
 
FPGA-Based Acceleration Architecture for Spark SQL Qi Xie and Quanfu Wang
FPGA-Based Acceleration Architecture for Spark SQL Qi Xie and Quanfu Wang FPGA-Based Acceleration Architecture for Spark SQL Qi Xie and Quanfu Wang
FPGA-Based Acceleration Architecture for Spark SQL Qi Xie and Quanfu Wang
 
Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
Analyzing 2TB of Raw Trace Data from a Manufacturing Process: A First Use Cas...
 
Migrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at FacebookMigrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at Facebook
 
File Format Benchmarks - Avro, JSON, ORC, & Parquet
File Format Benchmarks - Avro, JSON, ORC, & ParquetFile Format Benchmarks - Avro, JSON, ORC, & Parquet
File Format Benchmarks - Avro, JSON, ORC, & Parquet
 
Hive and Apache Tez: Benchmarked at Yahoo! Scale
Hive and Apache Tez: Benchmarked at Yahoo! ScaleHive and Apache Tez: Benchmarked at Yahoo! Scale
Hive and Apache Tez: Benchmarked at Yahoo! Scale
 
Hyperspace: An Indexing Subsystem for Apache Spark
Hyperspace: An Indexing Subsystem for Apache SparkHyperspace: An Indexing Subsystem for Apache Spark
Hyperspace: An Indexing Subsystem for Apache Spark
 
Interrupts on xv6
Interrupts on xv6Interrupts on xv6
Interrupts on xv6
 
Apache Arrow: High Performance Columnar Data Framework
Apache Arrow: High Performance Columnar Data FrameworkApache Arrow: High Performance Columnar Data Framework
Apache Arrow: High Performance Columnar Data Framework
 
0x003 - Exploiting LOLDrivers - Physical Memory Mayhem
0x003 - Exploiting LOLDrivers - Physical Memory Mayhem0x003 - Exploiting LOLDrivers - Physical Memory Mayhem
0x003 - Exploiting LOLDrivers - Physical Memory Mayhem
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Why your Spark Job is Failing
Why your Spark Job is FailingWhy your Spark Job is Failing
Why your Spark Job is Failing
 
AOT(Ahead Of Time)
AOT(Ahead Of Time)AOT(Ahead Of Time)
AOT(Ahead Of Time)
 
VHDL/FPGA
VHDL/FPGA VHDL/FPGA
VHDL/FPGA
 
Hive Bucketing in Apache Spark with Tejas Patil
Hive Bucketing in Apache Spark with Tejas PatilHive Bucketing in Apache Spark with Tejas Patil
Hive Bucketing in Apache Spark with Tejas Patil
 
Customizing the Document Library
Customizing the Document LibraryCustomizing the Document Library
Customizing the Document Library
 
Processing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeekProcessing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeek
 
MyRocks introduction and production deployment
MyRocks introduction and production deploymentMyRocks introduction and production deployment
MyRocks introduction and production deployment
 

Ähnlich wie WebSocket on client & server using websocket-sharp & ASP.NET Core

The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
Oleg Podsechin
 
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
Fabien Potencier
 

Ähnlich wie WebSocket on client & server using websocket-sharp & ASP.NET Core (20)

Dependent things dependency management for apple sw - slideshare
Dependent things   dependency management for apple sw - slideshareDependent things   dependency management for apple sw - slideshare
Dependent things dependency management for apple sw - slideshare
 
Rust Embedded Development on ESP32 and basics of Async with Embassy
Rust Embedded Development on ESP32 and basics of Async with EmbassyRust Embedded Development on ESP32 and basics of Async with Embassy
Rust Embedded Development on ESP32 and basics of Async with Embassy
 
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 
Devoxx France: Développement JAVA avec un IDE dans le Cloud: Yes we can !
Devoxx France: Développement JAVA avec un IDE dans le Cloud: Yes we can !Devoxx France: Développement JAVA avec un IDE dans le Cloud: Yes we can !
Devoxx France: Développement JAVA avec un IDE dans le Cloud: Yes we can !
 
The future of server side JavaScript
The future of server side JavaScriptThe future of server side JavaScript
The future of server side JavaScript
 
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
Golang 101 for IT-Pros - Cisco Live Orlando 2018 - DEVNET-1808
 
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!
 
Getting started with open mobile development on the Openmoko platform
Getting started with open mobile development on the Openmoko platformGetting started with open mobile development on the Openmoko platform
Getting started with open mobile development on the Openmoko platform
 
Improve your Java Environment with Docker
Improve your Java Environment with DockerImprove your Java Environment with Docker
Improve your Java Environment with Docker
 
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
GDGSCL - Docker a jeho provoz v Heroku a AWS
GDGSCL - Docker a jeho provoz v Heroku a AWSGDGSCL - Docker a jeho provoz v Heroku a AWS
GDGSCL - Docker a jeho provoz v Heroku a AWS
 
Plugins 2.0: The Overview
Plugins 2.0: The OverviewPlugins 2.0: The Overview
Plugins 2.0: The Overview
 
DevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChungDevFest 2022 - Cloud Workstation Introduction TaiChung
DevFest 2022 - Cloud Workstation Introduction TaiChung
 
Настройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'aНастройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'a
 
A glance at the Rust SWC
A glance at the Rust SWCA glance at the Rust SWC
A glance at the Rust SWC
 
oSC-2023-Cross-Build.pdf
oSC-2023-Cross-Build.pdfoSC-2023-Cross-Build.pdf
oSC-2023-Cross-Build.pdf
 
(phpconftw2012) PHP as a Middleware in Embedded Systems
(phpconftw2012) PHP as a Middleware in Embedded Systems(phpconftw2012) PHP as a Middleware in Embedded Systems
(phpconftw2012) PHP as a Middleware in Embedded Systems
 

Mehr von Chen Yu Pao

Mehr von Chen Yu Pao (9)

HoloLens 2的 MR(Mixed Reality)開發入門
HoloLens 2的 MR(Mixed Reality)開發入門HoloLens 2的 MR(Mixed Reality)開發入門
HoloLens 2的 MR(Mixed Reality)開發入門
 
SkiaSharp on Xamarin Forms
SkiaSharp on Xamarin FormsSkiaSharp on Xamarin Forms
SkiaSharp on Xamarin Forms
 
ReactiveUI Xamarin.Forms
ReactiveUI Xamarin.FormsReactiveUI Xamarin.Forms
ReactiveUI Xamarin.Forms
 
Xamarin Form using ASP.NET Core SignalR client
Xamarin Form using ASP.NET Core SignalR clientXamarin Form using ASP.NET Core SignalR client
Xamarin Form using ASP.NET Core SignalR client
 
使用JetBrains Rider開發Xamarin Forms
使用JetBrains Rider開發Xamarin Forms使用JetBrains Rider開發Xamarin Forms
使用JetBrains Rider開發Xamarin Forms
 
Xamarin ARKit Introduction 01
Xamarin ARKit Introduction 01Xamarin ARKit Introduction 01
Xamarin ARKit Introduction 01
 
Xamarin native forms
Xamarin native formsXamarin native forms
Xamarin native forms
 
Xamarin的Azure後端懶人包
Xamarin的Azure後端懶人包Xamarin的Azure後端懶人包
Xamarin的Azure後端懶人包
 
Proto actor 串接 Go 與 C# 簡易上手
Proto actor 串接 Go 與 C# 簡易上手Proto actor 串接 Go 與 C# 簡易上手
Proto actor 串接 Go 與 C# 簡易上手
 

Kürzlich hochgeladen

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

WebSocket on client & server using websocket-sharp & ASP.NET Core

  • 2. WebSocket ■ Duplex bi-directional Api for web server  client ■ RFC 6455 https://tools.ietf.org/html/rfc6455 ■ W3C standard: https://www.w3.org/TR/websockets / ■ Transport protocol intro: https://www.youtube.com/watch?v= 9FqjRN4VYUU ■ Js Client & protocol packet format intro: https://hpbn.co/websocket/
  • 3. WebSocket Connect State Transition 1. Connection Upgrade (Protocol Switch) 2. Data In/Out arbitrarily (Ping  Pong to detect online) 3. Close connection handshake Transfer “Frame” format
  • 5. Experiment with source!! (tracing/logging or make it debuggable)■ Client : websocket-sharp https://sta.github.io/websocket- sharp/ ■ Server : ASP.NET Core v2.1.1 Websocket https://docs.microsoft.com/en- us/aspnet/core/fundamentals/webso ckets?view=aspnetcore-2.1
  • 6. websocket-sharp ■ Run on .NET Framework 3.5 and above, Mono, Unity3D ■ A single DLL provide websocket client/server functionality, comply with RFC 6455 ■ HTTPS encrypt/decrypt algorithm is via Framework functionality ■ MIT license
  • 7. websocket-sharp ■ Client usage is very simple: https://github.com/sta/websocket- sharp#websocket-client ■ Has event hook let client handler websocket event easily: http://bit.ly/2ztELl9 ■ Mimic Js Websocket API: https://developer.mozilla.org/en- US/docs/Web/API/WebSocket
  • 8. ■ Entry Point: the “WebSocket” class: http://bit.ly/2L76Xf2 ■ WebSocket Connect() Implementation: – Connect() API : http://bit.ly/2KVjLsf  doHandshake() : http://bit.ly/2J9ySJn  setClientStream() : http://bit.ly/2zsdLCN  createHandshakeRequest() : http://bit.ly/2KKAkHO  sendHttpRequest() : http://bit.ly/2JeiYgX  checkHandshakeResponse() : http://bit.ly/2zsim7O => Create “NetworkStream (http://bit.ly/2L3q5xE)” for afterward R/W operation ■ WebSocket Send() Implementation: – Send() API : http://bit.ly/2LaYY0I  send(Opcode opcode, Stream stream, bool compressed) : http://bit.ly/2N31aYq  send(Fin fin, Opcode opcode, byte[] data, bool compressed) : http://bit.ly/2KPszQQ  sendBytes(byte[] bytes) : http://bit.ly/2zoQCkx => Wrap input into a lot of “WebSocketFrame (http://bit.ly/2N5zfHh)” then write to stream
  • 9. ■ WebSocket OnMessage event Implementation: – EventHandler<MessageEventArgs> OnMessage : ■ There’re two place emit the event: – messagec(MessageEventArgs e) : http://bit.ly/2L7etGO ■ This is binding to Action<> _message field, which is invoked On: – open() : http://bit.ly/2uaCGpl ■ Being called at Connect() API : http://bit.ly/2L65rgE – message() : http://bit.ly/2KMUt00 – startReceiving() : http://bit.ly/2NEcATw – open() : http://bit.ly/2zssHAE => Get data from internal _messageEventQueue.Dequeue() then invoke the event.
  • 10. ASP.NET Core Websocket Server ■ The whole implementation across many “Nuget Packges (https://www.nuget.org/packages)”: – Microsoft.AspNetCore.Websockets – Microsoft.AspNetCore.Http.Abstractions – Microsoft.AspNetCore.Http – Microsoft.AspNetCore.Http.Extensions – Microsoft.AspNetCore.Http.Features – Microsoft.AspNetCore.Server.Kestrel – Microsoft.AspNetCore.Server.Kestrel.Core – System.Net.WebSockets.WebSocketProtocol – System.Net.WebSockets ■ It’s impossible to get through whole source code simply by human 👀!!! ■ We have to “experiment it” with debuggable source code or being able to logging detail information.
  • 11. Build ASP.Net Core Framework Package (make it debuggable)■ Build debuggable ASP.NET Core nuget packages? 1. Clone the official build repo: Universe https://github.com/aspnet/Universe 2. Build it…...since ASP.NET Core module’s source has almost unified folder structure convention: ■ src: The real source code ■ test: Testing code ■ sample: sample project or test to verify production source ■ build: build configuration files ■ Write a example project to use those debuggable packages then run it, dive into….
  • 12.
  • 13. Build ASP.Net Core Framework Package ■ To build individual packages: 1. In Windows machine, Install chocolatey, git for windows, Visual Studio 2017 and ASP.NET workload, node.js (for npm): https://github.com/aspnet/Universe/wiki/Setting-a-machine-up-to-run-Universe 2. Clone Universe repo with correct tag ( -b 2.1.1), be sure to use --recursive to get associated git submodule repo in modules folder. 3. Run build.cmd (build.sh) on top folder to let ASP.NET Core Buildtools setup correct config files on first time, even if not all module can successful build. 4. Make sure the residue build process are all killed. 5. Switch to the module folder you want to build, delete anything in artifactsbuild subfolder, then use: build.cmd /p:CompileOnly=true /p:SkipTests=true To build those Nuget packages of the module, Resulting files will reside in module folder's artifactsbuild folder, and by default, they are debuggable.
  • 14. Build ASP.Net Core Framework Package (make it debuggable)■ Use “find –iname“ or “dir /s” to find Nuget packages real source location in Universe repo: ■ Exception: Microsoft.AspNetCore.Server.Kestrel Microsoft.AspNetCore.Server.Kestrel.Core is in modulesKestrelHttpServer folder ■ But the System.Net.WebSockets.WebSocketProtocol System.Net.WebSockets packages are .NET Core runtime’s built-in libraries, that belongs to the “CoreFx (https://github.com/dotnet/corefx)” repo, not in ASP.NET Core
  • 15. Time Savior: SourceLink support in VisualStudio (https://github.com/dotnet/sourcelink ) ■ Visual Studio 2017 v15.7 and above support SourceLink ( https://docs.microsoft.com/en- us/visualstudio/releasenotes/vs2017- relnotes#debug) , ■ Begin from ASP.NET Core 2.0 it support SourceLink too: https://github.com/dotnet/core/issues/897, but some packages may not ready: https://github.com/dotnet/buildtools/issues/ 1896 ■ Most of time it just works 👍
  • 16. ■ Caveat: – Can not use “GoToDefinition(F12)” if it hasn’t been using debugger dive into it. – If source code already disappear on GitHub, it cannot work. – Some too deep function(s) may not work (may be bug?!)
  • 17. Example experiment project ■ Example source repo: http://bit.ly/2KO3B4n ■ It use .NET Core SCD deployment (http://bit.ly/2maxCgj)to let ASP.NET core runtime use debuggable Nuget packages we created. ■ Runs only on Win10-x64 machine.
  • 18.
  • 19. Some interesting digging result: (1) ■ WebSocket connection setup is implmented in “DefaultWebSocketManager”, its AccetpWebSocketAsync() is forwarding Websocket upgrade connection work to WebSocketMiddleware: http://bit.ly/2ukREs1 ■ WebSocket SHA-1 encrypt key is hard-coded: http://bit.ly/2N2oTYT
  • 20. Some interesting digging result: (2) ■ The real “Connection Upgrade” phase is done in Microsoft.AspNetCore.Server.Kestrel nuget package’s code, which is Web Server itself: http://bit.ly/2umVGjz ■ And the WebSocket(http://bit.ly/2KO5uhK) instance is created from websocket middleware using “HttpResponseStream” as source, the real class is “ManagedWebSocket”, an internal un-documented Class:
  • 21. Some interesting digging result: (3) ■ The Actual ReadAsync() & WriteAsync() implementation entry point is extension methods: http://bit.ly/2uo2ZaL ■ Write data to websocket’s final operations is call HttpUpgradeStream’s WriteAsync(), which is also located in Web Server itself (the Microsoft.AspNetCore.Server.Kestrel.Core Nuget package) : http://bit.ly/2uaKk33 ■ Closing websocket implementation is done by ManagedWebSocket class itself: http://bit.ly/2mbw9pW
  • 24. Conclusion ■ ASP.NET Core use many Abstract class and Interface in its API, and the real implementation is DefaultOOXX most of the time. Ex: WebSocketManager  DefaultWebSocketManager HttpContext  DefaultHttpContext ■ ASP.NET Core’s Middleware is binding via “Microsoft.AspNetCore.Http.Features” Nuget’s code.
  • 25. Resources & Tools: ■ SourceGrpah: https://sourcegraph.com/ ■ VSCode & OmniSharp extension: https://code.visualstudio.com/Docs/languages/csharp ■ MDN: – Websocket API: https://developer.mozilla.org/en- US/docs/Web/API/Websockets_API – Writing WebSocket servers: https://developer.mozilla.org/en- US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers