Node.js 的 WebSocket 模块 ws.io

码农软件 · 软件分类 · Node.js 扩展 · 2019-04-15 15:13:19

软件介绍

A simple wrap to node.js ws module and then we can use it in a manner  like socket.io. It's really easy to use socket.io but it doesn't  support Blob yet. So I write a simple wrap to ws module for this  purpose. This is also a part of works from a contest by  ithelp.ithome.com.tw.

Features:

  1. trnasfer Blob/ArrayBuffer by put it in an object and emit it just as socket.io

  2. broadcast to all but not me

  3. emit to specified peer by socket.to(socket.id).emit('event', data)

  4. join/leave/in room

  5. in-memory store

Limits:

  1. can only send one Blob/ArrayBuffer within one emit

  2. the Blob/ArrayBuffer object must be a property of the emitting object in the first level, no deeper

  3. no configuration support

  4. no 3rd party data store support

  5. cannot scale (due to current sockets management and store design)

  6. client support is now through a static url: /ws.io/ws.io.js

install

npm install ws.io

usage

a simple echo server. (echo.js)

var app = require('http').createServer(handler),
io = require('./ws.io').listen(app);
app.listen(8443);

function handler (req, res) {
    res.setHeader('Content-Type', 'text/html');
    res.writeHead(200);
    res.end(
        "<!DOCTYPE html>"+
        "<html>"+
        "<head>"+
        "<script src='/ws.io/ws.io.js'></script>"+
        "<script>"+
        "var socket = io.connect('ws://localhost:8443');"+
        "socket.on('echo', function(data) {"+
        "    alert(data);"+
        "});"+
        "</script>"+
        "<body>"+
        "<button id='echo'>echo</button>"+
        "</body>"+
        "</html>"+
        "<script>"+
        "var button = document.getElementById('echo');"+
        "button.onclick = function() {"+
        "    socket.emit('echo', 'hello echo server.');"+
        "}"+
        "</script>"
    );
}

io.sockets.on('connection', function (socket) {
    socket.on('echo', function(data) {
        socket.emit('echo', data);
    });
});

a simple blob sharing through file api. (blob.js)

var fs = require('fs'),
url = require('url'),
app = require('http').createServer(function(req, res) {
    res.setHeader('Content-Type', 'text/html');
    res.writeHead(200);
    res.end(
        "<!DOCTYPE html>"+
        "<html>"+
        "<meta charset='utf-8'>"+
        "<head>"+
        "<style>"+
        "#panel {"+
        "    border: solid 1px #336699;"+
        "    line-height: 20px;"+
        "    vertical-align: middle;"+
        "    padding: 5px;"+
        "    border-radius: 5px;"+
        "}"+
        "</style>"+
        "<script src='/ws.io/ws.io.js'></script>"+
        "</head>"+
        "<body>"+
        "<input type='file' id='files'><br>"+
        "<div id='panel'><ul id='list'></ul></div>"+
        "</body>"+
        "</html>"+
        "<script>"+
        "var files = document.getElementById('files');"+
        "var socket = io.connect('ws://localhost:8443');"+
        "function getUrl() {"+
        "    if(!!window.URL) {"+
        "        return window.URL;"+
        "    }"+
        "    if(!!window.webkitURL) {"+
        "        return window.webkitURL;"+
        "    }"+
        "}"+
        ""+
        "files.addEventListener('change', function(e) {"+
        "    var URL = getUrl();"+
        "    if(files.files.length>0) {"+
        "        var file = files.files[0];"+
        "        if(file.type==='') {"+
        "            alert('File type unknown. Process stopped.');"+
        "            return false;"+
        "        }"+
        "        var src = URL.createObjectURL(file);"+
        "        var a = document.createElement('a');"+
        "        a.href = src;"+
        "        a.innerHTML = file.name;"+
        "        a.target = '_blank';"+
        "        var li = document.createElement('li');"+
        "        li.appendChild(a);"+
        "        document.getElementById('list').appendChild(li);"+
        "        socket.emit('share', {filename: file.name, type: file.type, file:file});"+
        "    }"+
        "});"+
        "var fileinfo;"+
        "socket.on('share', function(data) {"+
        "    var URL = getUrl();"+
        "    var a = document.createElement('a');"+
        "    var file = new Blob([data.file], {type:data.type});"+
        "    a.href = URL.createObjectURL(file);"+
        "    a.innerHTML = data.filename;"+
        "    a.target = '_blank';"+
        "    var li = document.createElement('li');"+
        "    li.appendChild(a);"+
        "    document.getElementById('list').appendChild(li);"+
        "});"+
        "</script>"
    );
}),
io = require('ws.io').listen(app);

io.sockets.on('connection', function(socket) {
    socket.on('share', function(data) {
        socket.broadcast.emit('share', data);
    });
});

app.listen(8443);

本文地址:https://codercto.com/soft/d/3641.html

智能Web算法

智能Web算法

Haralambos Marmanis、Dmitry Babenko / 阿稳、陈钢 / 电子工业出版社 / 2011-11 / 65.00元

本书涵盖了五类重要的智能算法:搜索、推荐、聚类、分类和分类器组合,并结合具体的案例讨论了它们在Web应用中的角色及要注意的问题。除了第1章的概要性介绍以及第7章对所有技术的整合应用外,第2~6章以代码示例的形式分别对这五类算法进行了介绍。 本书面向的是广大普通读者,特别是对算法感兴趣的工程师与学生,所以对于读者的知识背景并没有过多的要求。本书中的例子和思想应用广泛,所以对于希望从业务角度更好......一起来看看 《智能Web算法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具