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
にー兄さん
September 19, 2026
Programming
550
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 にー兄さん
ついにAuraも登場したのでJetpack XR SDKについて今一度語りたい / i wanna talk about xreal aura
drumath2237
0
47
『Ground Truth: How We Create Geometrically Accurate Reconstructions (Niantic Spatial Blog)』 を読む / read Ground Truth: How We Create Geometrically Accurate Reconstructions
drumath2237
0
80
この時代にOSS開発に向き合ってみる / face the oss development in this era
drumath2237
0
65
プログラマの職能が代替可能になりつつあるこの時代でプログラムと向き合う修行を積む / dedicating myself to the discipline of programming
drumath2237
0
140
個人的3D Gaussian Splattingニュースをご紹介 / sharing 3d gaussian splatting news
drumath2237
0
670
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
910
XRエンジニアの視点から XRのイマと社会実装の実現について考える / thinking-about-xr-popularization
drumath2237
0
99
軽率にプログラミング言語のシンタックスについて考えてみよう / 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
Security issues being discussed on Web Platforms
petamoriken
0
1.4k
mrbgem 三角測量 開発
ogom
0
200
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
350
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
480
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
7
1.9k
自サークルのIdPからログアウトできるようになるまで
taiseiue
0
110
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
450
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.8k
iOSDC2026登壇資料.pdf
riofujimon
0
210
すこし踏み込む CancellationToken
htkym
2
1.6k
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
120
setup-vp GitLab対応の裏側
naokihaba
0
150
Featured
See All Featured
Side Projects
sachag
456
43k
Evolving SEO for Evolving Search Engines
ryanjones
0
310
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
A designer walks into a library…
pauljervisheath
211
25k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Prompt Engineering for Job Search
mfonobong
0
480
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Designing for Performance
lara
611
70k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
320
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
Believing is Seeing
oripsolob
1
240
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
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