`
sillycat
  • 浏览: 2551692 次
  • 性别: 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





分享到:
评论

相关推荐

    javaweb应用程序源码-Websocket-Introduction-Course:在LinkedInLearnig课程使用JavaEE进

    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

    信息安全_数据安全_5_ZN2018_WV_-_Introduction_into_browser_hacking.pdf

    3. **SafeBrowsing绕过**:通过WebSocket(如L1kvID发现的644744)或其他方法,攻击者可以绕过SafeBrowsing保护机制。 **扩展API漏洞** 浏览器扩展API的不安全设计也可能成为攻击者的目标。例如,权限漏洞允许恶意...

    wspy:由Python实现的Websocket协议

    ## 1.Introduction这是客户端和服务器之间的新即时通信。 仅用于引用RFC6455HTML5 JavaScript API。 ## 2。用法这是python 2.7的软件包,您可以将其应用到自己的应用程序中,以构造服务器与浏览器之间的即时通信...

    ServerR:eek GeekApk RDBMS RESTFul + WebSocket API服务器应用程序

    Introduction 介绍 ServerR 是 GeekApk API 的实现,GeekApk 是一个 自由、开放、好玩 的 Android 社区 项目,从 起始,在自由度上超过酷安 GeekApk 里主要使用 4 个模型: 用户、应用、分类、评论 G

    JBoss7 introduction

    - WebSocket支持:JBoss AS 7 支持WebSocket协议,为实时双向通信提供了基础架构,有利于开发富互联网应用程序。 - 微内核架构:核心服务被最小化,只保留了运行应用服务器所必需的部分,其他功能以模块形式加载,...

    tomcat 8 官方英文原版 API 帮助文档 高清完整版

    下面提供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...

    tomcat 9 官方英文原版 API 帮助文档 高清完整版

    下面提供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...

    tomcat 7 官方英文原版 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) Classloading 12) JSPs 13) ...

    Java.EE.and.HTML5.Enterprise.Application.Development

    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 ...

    netxjs-introduction

    通过阅读`netxjs-introduction-lesson-1`这样的教程,初学者可以从基础开始逐步掌握这个库的使用方法,包括安装、基本API的调用以及简单的应用场景。 总的来说,Netx.js是JavaScript开发者在网络通信领域的一个有力...

    firstcup例程

    在《YourFirstCup: An Introduction to the Java EE Platform Release 7 for Java Platform, Enterprise Edition》这本官方文档中,Oracle公司为开发者们提供了一个全新的Java EE 7平台入门指南。这份文档的主要目的...

    RPA Infrastructure Training-参考答案.docx

    1. **TCP端口**:机器人与Orchestrator之间的通信依赖于特定的TCP端口。Orchestrator监听的端口用于接收机器人的连接请求。默认情况下,这通常是80或443端口,这两个端口分别对应HTTP和HTTPS服务,确保数据传输的...

    MQTT-CN.zip

    Introduction 第一章 - MQTT介绍 第二章 – MQTT控制报文格式 第三章 – MQTT控制报文 3.1 CONNECT – 连接服务端 3.2 CONNACK – 确认连接请求 3.3 PUBLISH – 发布消息 3.4 PUBACK –发布确认 3.5 PUBREC – 发布...

    Jump Start HTML5

    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...

    tomcat-7.0.90-developer.zip

    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).

    tomcat-7.0.90-windows-x64.zip

    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).

    Packt.Learning.Spring5

    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....

    Python asgi 框架开发指导文档

    - **介绍**(Introduction): 解释了ASGI的目的及其相对于WSGI的优势。 - **规范**(Specifications): 描述了ASGI的核心接口、数据结构以及如何使用这些接口进行通信。 - **扩展**(Extensions): 讨论了ASGI...

Global site tag (gtag.js) - Google Analytics