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
580
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
85
この時代にOSS開発に向き合ってみる / face the oss development in this era
drumath2237
0
68
プログラマの職能が代替可能になりつつあるこの時代でプログラムと向き合う修行を積む / dedicating myself to the discipline of programming
drumath2237
0
140
個人的3D Gaussian Splattingニュースをご紹介 / sharing 3d gaussian splatting news
drumath2237
0
680
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
930
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
気持ちぃ〜角丸 Squircle
ryu1013
0
270
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
380
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
760
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
3k
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
370
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
160
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
160
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
170
AWSに止められる覚悟してますか?
morizo_1984
2
530
Featured
See All Featured
Designing for Timeless Needs
cassininazir
1
520
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
400
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
440
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
450
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
580
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Building the Perfect Custom Keyboard
takai
2
900
Ruling the World: When Life Gets Gamed
codingconduct
0
400
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Exploring anti-patterns in Rails
aemeredith
4
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