2016年6月17日 星期五

Websocket的解決方案:Client Browser使用 Javascript,Server使用python或node.js


Websocket 是一種Internet上資料傳送與接收的一個新技術。只要能掌握這個技術,就可以實現許多需要透過網路傳送資料的應用程式。

Server: python(tornado), node.js(socket.io, ), ...
Client(Browser): javascript (socket.io)

是否可以將websocket Client連上 websocket server,而不論server是用哪一種語言實現的。其實理論上是必然可行的,因為websocket只是一個傳輸資料的protocol,因此python, node.js等等畢竟都只是開發的工具而已,只要傳送/接收的資料符合protocol,那資料理論上是可以傳輸無阻的。

這篇文章是一個學習記錄的過程,目的就是要實驗這個理論,並且更熟悉實務上會用到哪些函式庫,以及這此函式庫是否有什麼使用上的限制。網路上有非常多的資源說明怎麼架設websocket server/client,但是通常都是單一方法的說明。

環境說明:Server端架設在AWS EC2 上

1. 在server端使用python 搭配tornado library架設websocket server
2. 在client端建立websocket client
3. 在server端改使用node.js 架設websocket server
4. 在server, client端使用node.js中使用socket.io library

1. 在server端使用python 搭配tornado library架設websocket server
[source code]websocket server written in python using tornado module

2. 在client端建立websocket client
[source code]websocket client written in html/javascript 

3. 在server端改使用node.js 架設websocket server
[source code]websocket server written in node.js
[source code]修改後的websocket client written in html/javascript

4. 在server, client端使用node.js中使用socket.io library
[source code]websocket server written in node.js with socket.io module
[source code]websocket client using socket.io module

references: [1]使用 Node.js 與 Socket.IO 建立即時性(Realtime)網頁應用程式 App