socket.io & a C# Client Library

I.T. 9 遊戲日誌
4 min readJun 6, 2019

--

Once a wonderful solution, yet…不敵時間巨輪而成為被遺棄的技術(之一)

Scroll to the end if you just need a C# socket.io client
若只需要一個C# socket.io客戶端,請看最底

Sometimes wheel must be reinvented / 有時候得就重造輪子 (source 來源)

Some boring history
一點點無趣的歷史

Once upon a time, scripts on a website could retrieve other resources only through <iframe> or JSONP, until Microsoft introduced The Best™️ technology ever in IE5 — MSXML, which later becomes XMLHttpRequest (XHR) and was the only legitimate reason to allow ActiveX execution. (Don’t tell me about Flash)

在上個世紀,JavaScript只可以通過<iframe>或JSONP來加載其他網絡資源,直到微軟在IE5裏內置了一件有史以來做過最棒™️的技術 — MSXML,亦即是XMLHttpRequest (XHR)的前身,而它是使用ActiveX的唯一合法原因。(別提Flash)

All of a sudden, data-interactive web content blossomed and was no longer reserved for sci-fi movie.

突然之間,能進行數據互動功能的網站如雨後春笋,不再局限於科幻片中。

Then by 201x, CORS got standardized and we were able to get away from the ugly JSONP trick. Yet, XHR could still only do polling and two-way communication was still not a thing after so many years.

到201x年,CORS標準化讓大家不再需要依賴醜陋的JSONP技倆。但,XHR基本上還只是限於輪詢使用,真正雙向通訊在這麼多年後依然沒有解。

Of course, readers from 2019 know what’s happened. WebSocket (RFC 6455) is here and is supported by all modern browsers. Finally, two-way communication!

當然來自2019年的讀者就很清楚故事的後續。終於等到WebSocket (RFC 6455)並且得到瀏覽器的廣泛支持。雙向通訊,我等了你好久。

Hm…Did we miss something?
是不是好像忘了什麼…

Hello?
もしもし?

socket.io

When the currently known WebSocket was still being invented, Guillermo Rauch and Automattic (according to Wikipedia and Github LICENSE file) invented socket.io as a bridging solution. It provides two-way communication (as least perceived if not technically accurate), and has a reference NodeJS server and JavaScript client implementation.

當現今世上所知道的WebSocket還在被發明的同時,Guillermo Rauch和Automattic (名字跟據Wikipedia和Github LICENSE檔)發明了socket.io作為過渡解決方案。socket.io提供了雙向通訊功能 (儘管技術上未必是但起碼用起來是這樣),並且有一個NodeJS伺服器和JavaScript客戶端的實作。

socket.io does the magic by using different kind of underlying mechanisms, such as XHR and once-were-still-only-a-spec-WebSocket. It also enforces more semantic in communication instead of just letting raw bytes/characters flow through, which is both good or bad depends on who you ask.

socket.io通過各種低層通訊渠道去完成使命,包括XHR和當時尚在紙上談兵的WebSocket。它還在通訊渠道做了更多語意規則,而不只是單純的交換一堆位元組/字元,至於這個設計是好是壞就得視乎你在問誰。

But since WebSocket has now been standardized, many other socket.io client and server projects went dead. In my opinion, the envisioned but somewhat-over-designed architecture (4 layers — socket.io and its parser, then engine.io and its parser) deters people, if not just me, from making and maintaining implementations that truly obey the design document.

但自WebSocket標準出爐以來,很多socket.io的第三方專案都被遺棄。在我看來,那個看似很有先見但某程度上是過度設計的分層 (4層 — socket.io和它的parser,然後engine.io和它的parser),絕對讓人提不起勁去完全按照原文檔所設計的開發和維護一個socket.io實作。

socket.io architecture / socket.io架構圖

That design reminds me of Java and the-IBM-way-of-implementing things. For example, to create some horses, you would need a Horse class as usual, but you would be using HorseFactory to create it. You didn’t forget to code that, did you? And also an IHorse interface too, remember the implementation should be replaceable! But hey, what we really want is just a simple two-way communication protocol!

這樣的設計讓我聯想起Java和IBM式的架構。例如若要創建若干隻馬,首先如常需要一個Horse類別,然後實際上則要用HorseFactory類別來構建這些馬,你碰沒有忘記這個吧?當然還有IHorse介面,你應知道實作本身應該是可以替換的。…喂,但很多想要的僅僅只是個雙向通訊而已!

How to build a horse 1. 你想構建一匹馬 2. 但你得先構建一個造馬工廠

And here is the IBM’s C# socket.io client, I am not disappointed!

然後這裏是IBM所作的socket.io C#客戶端,實在沒有令我失望。

If this is not enough, recall that the socket.io server reference implementation were coded in NodeJS. It might be the best tool-chain that offers coop-multitasking, before Promise, Async-await, etc becomes popular in other offerings. Yet, I need not to say NPM is what everyone loves and hates, probably more on the hate side.

如果這還未夠,再說一次socket.io範例server是以NodeJS寫成的。在「那些年」要做協程多工的話,NodeJS也許真的是最好的工具,但這都是在Promise、Async-await等等標準化和成為熱門之前的事。而且,NPM讓大家又愛又恨這一點估計不用我來說了吧,也許,是恨多於愛吧。

編程迷因 programeme

By the way, socket.io is not the only thing we have forgotten, there were dozens more hacks to emulate two-way communication.

順帶一提,socket.io並不是唯一一個被遺忘的,這裏還記載了N種我們為了模擬出雙向通訊而曾用過的雕蟲小技。

Problem Statement
問題描述

Oh my. It is my turn to integrate with a not-so-old system with data stream coming through a socket.io server, and I have to use C# to receive that. For different reasons, I couldn’t get any existing socket.io implementations up and working, plus most of them are not maintained since 2014. They are surprisingly standard compliant (i.e. too complex for the reasons listed above) such that I don’t feel like debugging them.

天啊。今天換到我要對接一個「不是太舊」的系統,而它的資料是通過socket.io串流過來且而我必須用C#接收。各個我在網上搜到的socket.io程式庫都各有問題,使我用不上,更不用說它們大都在2014年起就沒有再維護。而各個程式庫都出奇地貼近標準所寫成 (即是因上述提到的原因而搞得太複雜),讓我實在沒有興致debug它們…

So I did what a software engineer should do — reinvent the wheel.

我做了IT9應做的事 — 重造輪子。

Just one file, no factory, no fuss, it offers the most basic functionalities and pretty much self-explanatory. If it happens that you need a simple C# socket.io client and one that doesn’t take you more than 5 minutes to understand, please feel free to take it and do whatever you need.

一個檔案,沒有Factory,沒有別的。它實現了最基礎的功能,事實上用法都應該不言而諭。如果你碰巧需要一個簡單的C# socket.io客戶端,而且一個不需要分5分鐘就能到的,歡迎取用。

--

--

I.T. 9 遊戲日誌
I.T. 9 遊戲日誌

Written by I.T. 9 遊戲日誌

「IT9,你的資訊真的很有用」 你好 我就係IT9 Trust me I am IT9 // fb@it9gamelog, youtube@it9gamelog

No responses yet