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
分享到:
相关推荐
java ...1 Introduction to the project application 2 Deployment Java EE WebSocket API 1 Websocket Methodologies 2 WebSocket functionality 3 Annotation methodology 4 Implement WebSocke
## 1.Introduction这是客户端和服务器之间的新即时通信。 仅用于引用RFC6455HTML5 JavaScript API。 ## 2。用法这是python 2.7的软件包,您可以将其应用到自己的应用程序中,以构造服务器与浏览器之间的即时通信...
Introduction 介绍 ServerR 是 GeekApk API 的实现,GeekApk 是一个 自由、开放、好玩 的 Android 社区 项目,从 起始,在自由度上超过酷安 GeekApk 里主要使用 4 个模型: 用户、应用、分类、评论 G
Chapter 1 : Java Platform, Enterprise Edition Introduction Deliverables What’s New in Java EE 7 Chapter 2 : Servlets WebServlet Servlet Filters Event Listeners Asynchronous Support ...
下面提供api目录: 1) Introduction 2) Setup 3) First webapp 4) Deployer 5) Manager 6) Host Manager 7) Realms and AAA 8) Security Manager 9) JNDI Resources 10) JDBC DataSources 11) Class...
下面提供api目录: 1) Introduction 2) Setup 3) First webapp 4) Deployer 5) Manager 6) Host Manager 7) Realms and AAA 8) Security Manager 9) JNDI Resources 10) JDBC DataSources 11) Class...
1) Introduction 2) Setup 3) First webapp 4) Deployer 5) Manager 6) Host Manager 7) Realms and AAA 8) Security Manager 9) JNDI Resources 10) JDBC DataSources 11) Classloading 12) JSPs 13) ...
Chapter 1 Introduction to Java EE and HTML5 Enterprise Development Chapter 2 Persistence Chapter 3 RESTful Resources Chapter 4 WebSocket Chapter 5 HTML5, JavaScript, and CSS Chapter 6 ...
Introduction 第一章 - MQTT介绍 第二章 – MQTT控制报文格式 第三章 – MQTT控制报文 3.1 CONNECT – 连接服务端 3.2 CONNACK – 确认连接请求 3.3 PUBLISH – 发布消息 3.4 PUBACK –发布确认 3.5 PUBREC – 发布...
Chapter 1. Basics: What is HTML5? Chapter 2. Basics: The Anatomy of HTML5 Chapter 3. Basics: Structuring Documents Chapter 4. Basics: HTML5 Forms Chapter 5. Basics: Multimedia, Audio and Video Chapter...
The Apache Tomcat® software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and ...[Java Community Process](https://jcp.org/en/introduction/overview).
The Apache Tomcat® software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and ...[Java Community Process](https://jcp.org/en/introduction/overview).
Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection....
Introduction 在某些如服务器需要即时推送消息给客户端的情况下,会使用WebSocket以实现双向通讯。此时,对于其他原本使用HTTP的请求和数据交互,也可以借用WebSocket来处理,以提高性能,减少流量。 restful-socket...
With this practical guide, Matt Stauffer–a leading teacher and developer in the Laravel community–provides the definitive introduction to one of today’s most popular web frameworks. The book’s ...
Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection....
Implementing Socket IPC and WebSocket Server/Client Multicasting in RxPHP and PHP7 pthreads Extension Multithreaded and Distributed Computing with pthreads and Gearman Using Advanced Operators and ...
Loggrove 是对本地、远程日志文件进行 分页阅读、实时阅读(websocket)、关键词匹配、统计、监控、钉钉告警、Highcharts趋势图展示 的 Web 平台服务,并包含 用户认证、LDAP认证、操作审计 等基础服务。 DEMO 地址...
:grinning_face_with_smiling_eyes:Introduction 一个在线聊天系统,提供实时聊天,账户管理,好友管理,聊天记录管理,聊天群组,朋友圈,相册等功能。 后台使用Java,tomcat,mysql,servlet,jsp,前端使用html,js,...
我们使用轻量级的Rust websocket实现tungstenite (包含在Binance库中)来实现稳定的价格流。 :check_mark_button: 接下来,我们要计算套利潜力-快速并包含费用。 这最终是简单的算术运算,我们可以轻松地阅读...