Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないので...
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
にー兄さん
September 19, 2026
Programming
650
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
C# Kaigi 2026でLT登壇した資料です
https://csharpkaigi.net/talks/lt-unity-websocket
にー兄さん
September 19, 2026
More Decks by にー兄さん
See All by にー兄さん
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
95
ついにAuraも登場したのでJetpack XR SDKについて今一度語りたい / i wanna talk about xreal aura
drumath2237
0
53
『Ground Truth: How We Create Geometrically Accurate Reconstructions (Niantic Spatial Blog)』 を読む / read Ground Truth: How We Create Geometrically Accurate Reconstructions
drumath2237
0
88
この時代にOSS開発に向き合ってみる / face the oss development in this era
drumath2237
0
69
プログラマの職能が代替可能になりつつあるこの時代でプログラムと向き合う修行を積む / dedicating myself to the discipline of programming
drumath2237
0
140
個人的3D Gaussian Splattingニュースをご紹介 / sharing 3d gaussian splatting news
drumath2237
0
710
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
950
XRエンジニアの視点から XRのイマと社会実装の実現について考える / thinking-about-xr-popularization
drumath2237
0
110
軽率にプログラミング言語のシンタックスについて考えてみよう / lets-think-about-programming-lang-syntax
drumath2237
0
150
Other Decks in Programming
See All in Programming
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.9k
Railsバージョンを4倍にした話
optim
0
800
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
220
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
210
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
4
1.1k
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
240
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
200
golang-bob.pdf
iwatsukayura
0
140
Next.js: The final boss of Web Performance
guaca
0
350
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
200
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
190
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
12
4.9k
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
900
WENDY [Excerpt]
tessaabrams
14
40k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
The agentic SEO stack - context over prompts
schlessera
0
980
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Designing Powerful Visuals for Engaging Learning
tmiket
1
620
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Odyssey Design
rkendrick25
PRO
2
890
Transcript
Unityで System.Net.WebSockets な WebSocketサーバが動かないので Unity Monoのコードを覗いてみた にー兄さん@ninisan_drumath C# Kaigi 2026
にー兄さん ソフトウェアエンジニア@HoloLab inc. Babylon.js勉強会運営 Iwaken Lab. Microsoft MVP for Developer
Technologies (2024~) 好きな技術 Babylon.js / WebXR Device API / Android XR / 3D Gaussian Splatting @ninisan_drumath @drumath2237
本日の資料は撮影・SNS共有OKです📸 (公開予定)
サンプルプロジェクト https://github.com/drumath2237/Unity-SystemWSServer-Playground
アジェンダ 1. Unity/WSサーバにおける 技術選定の背景 2. Unity Monoのコードを見てみよう 3. おまけ:CoreCLRでも試してみる
Unity/WSサーバにおける 技術選定の背景
UnityでWebSocketサーバを動かす選択肢 検索して出てくるのは Fleck statianzo/Fleck NuGetで配布されているC#のWebSocket実装 最終更新は5年前 websocket-sharp sta/websocket-sharp 老舗のWebSocket実装ライブラリ NuGetへの最終リリースは10年前だけどリポジトリはちょくちょく更新かかってるっぽい
NativeWebSocket endel/NativeWebSocket 一番モダンな選択肢になりそうだけどあんまり記事で見ない Unity以外にも色んなプラットフォームに対応
WSサーバって 標準ライブラリだけで実装できないっけ……? → HttpListenerとWebSocketが使える
実装のイメージ HttpListener で接続リクエストを WebsoketContext にUpgrade var _listener = new HttpListener();
// ... var context = await _listener.GetContextAsync(); HandleClientAsync() 内でWebSocketの受信・送 信 while (socket.State == WebSocketState.Open) { var result = await socket.ReceiveAsync( buffer, token ); var message = Encoding.UTF8.GetString( buffer, 0, result.Count ); if (!context.Request.IsWebSocketRequest) { context.Response.StatusCode = 400; context.Response.Close(); return; } オウム返しするだけ var wsContext = await context.AcceptWebSocketAsync(null); var socket = wsContext.WebSocket; _ = HandleClientAsync(socket, token); } // var response = Encoding.UTF8.GetBytes( $"Echo: {message}" ); await socket.SendAsync( response, WebSocketMessageType.Text, true, token );
これらは.NET Standard 2.0/2.1でサポート Unityでも動きそうな気がする var context = await _listener.GetContextAsync(); if
(!context.Request.IsWebSocketRequest) // { context.Response.StatusCode = 400; context.Response.Close(); continue; } なぜか上手く判定されない ここで // NotImplementedException var wsContext = await context.AcceptWebSocketAsync(null); var socket = wsContext.WebSocket; _ = HandleClientAsync(socket, token); HttpListenerContext が上手く動いてくれない
(動作画面)
Unity Monoのコードを見てみよう
Unity-Technologies/mono Unity Technologiesのリポジトリに Monoのフォークがある ディレクトリ構成 mono ├─ mono/ <------------------- Mono
Runtime ├─ mcs/ │ ├─ mcs/ <------------- Mono C# │ ├─ class/ │ │ ├─ referencesource/ .NET Core │ │ ├─ System/ C# │ │ └─ ... │ └─ ... └─ ... のコア実装 コンパイラの実装 参考用の のコードがあるらしい よく見る クラスライブラリのコード
実際のコードを見てみる mcs/class/System/System.Net/HttpListenerRequest.cs うまく動作しなかった部分を見ると、 [MonoTODO] というアトリビュートがついていた 実装が不十分なコードになっているっぽい? 実際のコードを確認することで Unity Monoで動作しない裏付けが取れた [MonoTODO]
public bool IsWebSocketRequest { get { return false; } } mcs/class/System/System.Net/HttpListenerContext.cs [MonoTODO] public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync (string subProtocol) { throw new NotImplementedException (); }
おまけ:CoreCLRでも試してみる
Unity 6.7とCoreCLR Unity 6.7(現時点ではα版)から ビルドモジュールからCoreCLRビルドサポートを 追加できるように まだExperimental 6.8からエディタ・デスクトップPlayerの 正式リリース予定
動いた!
まとめ Unity×WSサーバ実装の選択肢でやたらサードパーティが挙がることに疑問を持った 最新の正式リリース(6.6)の状態で System.NetスタックなWebSocketサーバ実装は一筋縄ではいかない Unity Monoのコードを見てみると、 [MonoTODO] 属性がついている未実装部分だということが分かった CoreCLR (experimental)なscripting
backendでビルドすると HttpListenerとWebSocketによるWSサーバが動いた
関連・参考 HttpListener and Websockets(Unity Discussion) https://discussions.unity.com/t/httplistener-and-websockets/820948 Websocket Server in Standalone
build(Unity Discussion) https://discussions.unity.com/t/websocket-server-in-standalone-build/832108 Unity で System.Net.WebSockets を使ったゲームサーバー書こうとしたらうまくいかない(noir_neo’s blog) https://noir-neo.hatenablog.com/entry/2018/03/17/001718 Path to CoreCLR, 2026: Upgrade Guide(Unity Discussion) https://discussions.unity.com/t/path-to-coreclr-2026-upgrade-guide/1714279