`
sillycat
  • 浏览: 2486686 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

2018 WebSocket(1)Introduction

 
阅读更多
2018 WebSocket(1)Introduction

For HTTP, the connection only can be triggered by Clients. One-way, request/response, stateless.

For WebSocket, on top of TCP, binary/text, protocol prefix is ws, WSS for SSL.

Client Side API
var ws = new WebSocket(‘ws://localhost:8080');
ws.readyState return value will identify the status
CONNECTING 0
OPEN  1 connected successfully
CLOSING 2 closing the connection
CLOSED 3

onopen event is the call back for connected successfully
ws.onopen = function(){
    ws.send(“Hello Server!");
}

webSocket.onclose
ws.onclose = function(event){
};
ws.addEventListener(“close”, function(event){
    var code = event.code; var reason = event.reason; var wasClean = event.wasClean;
});

webSocket.onmessage
ws.onmessage = function(event){
    var data = event.data; //it can be text, event.data === string, or event.data instanceof ArrayBuffer, Blob
};
ws.binaryType = “blob”;
ws.binaryType = “arraybuffer”;

We can check the bufferAmount to see how many we sent to server.

webSocket.onerror()

Server Side API
3 popular implementation
https://github.com/uNetworking/uWebSockets
https://github.com/theturtle32/WebSocket-Node a little old
https://socket.io/

A very Powerful Server
http://websocketd.com/
Install that on my MAC, download the file websocketd-0.3.0-darwin_amd64.zip
Unzip that and place in the working directory and add to path
After installation, check the version
> websocketd --version
websocketd 0.3.0 (go1.9.2 darwin-amd64) --

How it works

It is said that the standard input and output will be used in web socket.
I just tried the easiest demo

For example, for Server side:
> websocketd --port=8080 ls -l

This will start a deamon to dir the directory for each request

Client Demo, Open file in chrome, file:///Users/hluo/work/websocket/index.html, the content for that file is as follow:
> cat index.html
<html>
<head><title>Client Websocket</title></head>
<body>
Websocket Client
<script>
var ws = new WebSocket('ws://localhost:8080/');
                ws.onmessage = function(event) {
                  console.log('Count is: ' + event.data);
                };
</script>
</body>
</html>

Some counter example I tried.
For BASH
> cat counter.sh
#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
echo $COUNT
sleep 0.5
done

For python
> cat counter.py
#!/usr/bin/python
from sys import stdout
from time import sleep
for count in range(0, 10):
print(count + 1)
stdout.flush()
sleep(0.5)

> websocketd --port=8080 python counter.py

For Java
> cat Counter.java
class Counter {
public static void main(String[] args) throws Exception {
for(int i = 0; i < 10; i++){
System.out.println(i);
Thread.sleep(500);
}
}
}

I need to javac the java to class
>javac Counter.java
> websocketd --port=8080 java Counter


More example here
https://github.com/joewalnes/websocketd/tree/master/examples


References:
http://www.ruanyifeng.com/blog/2017/05/websocket.html





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics