最新消息:

WebSocket和websocketd

工作 admin 2486浏览 0评论

WebSocket是什么

WebSocket是HTML5下面的一种技术,设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力。 浏览器通过 JavaScript 向服务器发出建立 WebSocket 连接的请求,连接建立以后,客户端和服务器端就可以通过 TCP 连接直接交换数据。因为 WebSocket 连接本质上就是一个 TCP 连接,所以在数据传输的稳定性和数据传输量的大小方面,和轮询以及 Comet 技术比较,具有很大的性能优势。

简单的说这就是一个可以通过web来访问的socket协议。

具体可参考以下:
https://www.websocket.org/
https://developer.mozilla.org/en-US/docs/WebSockets

websocketd是什么

websocketd是一个简单的websocket服务Server,运行在命令行方式下,可以通过websocketd和已经有程序进行交互。

你可以写一个程序通过读写STDIN和STDOUT来和websocketd交互。因为是标准的读写接口,所以你的程序可以使用任何语言来处理。
What is WebSocket?

The WebSocket specification—developed as part of the HTML5 initiative—introduced the WebSocket JavaScript interface, which defines a full-duplex single socket connection over which messages can be sent between client and server. The WebSocket standard simplifies much of the complexity around bi-directional web communication and connection management.

WebSocket represents the next evolutionary step in web communication compared to Comet and Ajax. However, each technology has its own unique capabilities. Learn how these technologies vary so you can make the right choice.

这个程序的项目地址如下:
https://github.com/joewalnes/websocketd

websocketd 10秒教程

1.下载并安装websocketd

其实就是把websocketd下载到本地任何目录,反正你知道运行方式就可以,方便的话你可以放到

/usr/local/bin

目录下。直接在项目地址下载最新的就可以了。

2.写一个程序把数据显示到STDOUT

你可以用任何语言。
例子用了最简单的sh。
count.sh:

#!/bin/bash
# Count from 1 to 10, pausing for a second between each iteration.
for COUNT in $(seq 1 10); do
    echo $COUNT
    sleep 1
done

记得让他变成可执行文件:

$ chmod +x ./count.sh

3.启动websocketd server

$ websocketd --port=8080 ./count.sh

4.用javascript写一个客户端来交互

count.html:

<!DOCTYPE html>
<pre id="log"></pre>
<script>
  // helper function: log message to screen
  function log(msg) {
    document.getElementById('log').textContent += msg + '\n';
  }

  // setup websocket with callbacks
  var ws = new WebSocket('ws://localhost:8080/');
  ws.onopen = function() {
    log('CONNECT');
  };
  ws.onclose = function() {
    log('DISCONNECT');
  };
  ws.onmessage = function(event) {
    log('MESSAGE: ' + event.data);
  };
</script>

很简答,建立连接,然后接收消息,当然你也可以发送消息。

5.websocket的常用API

onopen
onerror
onclose
onmessage
send

转自:http://jianshu.io/p/63afd0099565

——————————————————————————————————————–

websocketd

websocketd is a small command-line tool that will wrap an existing command-line interface program, and allow it to be accessed via a WebSocket.

WebSocket-capable applications can now be built very easily. As long as you can write an executable program that reads STDIN and writes to STDOUT, you can build a WebSocket server. Do it in Python, Ruby, Perl, Bash, .NET, C, Go, PHP, Java, Clojure, Scala, Groovy, Expect, Awk, VBScript, Haskell, Lua, R, whatever! No networking libraries necessary.

Details

Upon startup, websocketd will start a WebSocket server on a specified port, and listen for connections.

Upon a connection, it will fork the appropriate process, and disconnect the process when the WebSocket connection closes (and vice-versa).

Any message sent from the WebSocket client will be piped to the process’s STDIN stream, followed by a \n newline.

Any text printed by the process to STDOUT shall be sent as a WebSocket message whenever a \n newline is encountered.

Download

Download for Linux, OS X and Windows

Quickstart

To get started, we’ll create a WebSocket endpoint that will accept connections, then send back messages, counting to 10 with 1 second pause between each one, before disconnecting.

To show how simple it is, let’s do it in Bash!

count.sh:

#!/bin/bash
for COUNT in $(seq 1 10); do
  echo $COUNT
  sleep 1
done

Before turning it into a WebSocket server, let’s test it from the command line. The beauty of websocketd is that servers work equally well in the command line, or in shell scripts, as they do in the server – with no modifications required.

$ chmod +x count.sh
$ ./count.sh
1
2
3
4
5
6
7
8
9
10

Now let’s turn it into a WebSocket server:

$ websocketd --port=8080 ./count.sh

Finally, let’s create a web-page that to test it.

count.html:

<!DOCTYPE html>
<pre id="log"></pre>
<script>
  // helper function: log message to screen
  function log(msg) {
    document.getElementById('log').textContent += msg + '\n';
  }

  // setup websocket with callbacks
  var ws = new WebSocket('ws://localhost:8080/');
  ws.onopen = function() {
    log('CONNECT');
  };
  ws.onclose = function() {
    log('DISCONNECT');
  };
  ws.onmessage = function(event) {
    log('MESSAGE: ' + event.data);
  };
</script>

Open this page in your web-browser. It will even work if you open it directly from disk using a file:// URL.

More Features

  • Very simple install. Just download the single executable for Linux, Mac or Windows and run it. No dependencies, no installers, no package managers, no external libraries. Suitable for development and production servers.
  • Server side scripts can access details about the WebSocket HTTP request (e.g. remote host, query parameters, cookies, path, etc) via standard CGI environment variables.
  • As well as serving websocket daemons it also includes a static file server and classic CGI server for convenience.
  • Command line help available via websocketd --help.
  • Includes WebSocket developer console to make it easy to test your scripts before you’ve built a JavaScript frontend.
  • Examples in many programming languages are available to help you getting started.

User Manual

More documentation in the user manual

Example Projects

Got more examples? Open a pull request.

My Other Projects

 

 

 

 

转载请注明:jinglingshu的博客 » WebSocket和websocketd

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址