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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
にー兄さん
September 19, 2026
Programming
620
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
50
『Ground Truth: How We Create Geometrically Accurate Reconstructions (Niantic Spatial Blog)』 を読む / read Ground Truth: How We Create Geometrically Accurate Reconstructions
drumath2237
0
86
この時代に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
690
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
940
XRエンジニアの視点から XRのイマと社会実装の実現について考える / thinking-about-xr-popularization
drumath2237
0
100
軽率にプログラミング言語のシンタックスについて考えてみよう / 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
ActionController meets Protocol Buffers
sorah
0
650
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
220
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
160
golang-bob.pdf
iwatsukayura
0
130
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
170
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
510
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
130
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
2
1.1k
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
820
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
3
960
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.7k
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
130
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Making Projects Easy
brettharned
120
6.8k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.8k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
940
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
290
KATA
mclloyd
PRO
35
16k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
610
Done Done
chrislema
187
17k
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