2018 WebSocket(1)Introduction

本文介绍了WebSocket的基本概念及其在客户端和服务端的应用。WebSocket建立在TCP之上,提供了全双工通信能力,适用于实时交互应用。文章详细解释了如何使用JavaScript API创建WebSocket连接,并通过示例展示了不同编程语言如何发送和接收消息。
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值