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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
にー兄さん
September 19, 2026
Programming
1
0
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 にー兄さん
ついにAuraも登場したのでJetpack XR SDKについて今一度語りたい / i wanna talk about xreal aura
drumath2237
0
41
『Ground Truth: How We Create Geometrically Accurate Reconstructions (Niantic Spatial Blog)』 を読む / read Ground Truth: How We Create Geometrically Accurate Reconstructions
drumath2237
0
77
この時代にOSS開発に向き合ってみる / face the oss development in this era
drumath2237
0
63
プログラマの職能が代替可能になりつつあるこの時代でプログラムと向き合う修行を積む / dedicating myself to the discipline of programming
drumath2237
0
110
個人的3D Gaussian Splattingニュースをご紹介 / sharing 3d gaussian splatting news
drumath2237
0
650
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
850
XRエンジニアの視点から XRのイマと社会実装の実現について考える / thinking-about-xr-popularization
drumath2237
0
96
軽率にプログラミング言語のシンタックスについて考えてみよう / lets-think-about-programming-lang-syntax
drumath2237
0
150
エンジニアが軽率に趣味から始める、OSS貢献を軸とした個人活動 / oss-contribution-as-a-hoby-project
drumath2237
0
110
Other Decks in Programming
See All in Programming
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
570
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
110
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
260
Go のフラットな パッケージに 「ファイル単位の private」を 〜Linter “declscope” を作った話〜
mpyw
0
320
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.2k
変化を抱擁するドキュメントの作り方 - ビジネスルール駆動開発がもたらす、コードとの新しい関係
ioki
2
200
新卒PdEのリアル
ryu1013
1
500
スマート反転とウェブアクセシビリティ
camiha
0
210
wkhtmltopdfの次どうするか問題2026
willnet
1
100
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
280
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
190
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
330
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
540
Making the Leap to Tech Lead
cromwellryan
135
10k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
240
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Speed Design
sergeychernyshev
33
2.1k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
800
Building Flexible Design Systems
yeseniaperezcruz
330
41k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
420
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
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