简体   繁体   English

如何编写websocket客户端

[英]How to write a websocket client

I'm trying to unit test my websocket on node.js and want to mock out a websocket client. 我正在尝试在node.js上对我的websocket进行单元测试,并且想要模拟一个websocket客户端。 I could create a HTML file that just connects to my server but then I can't run a single test on the server. 我可以创建一个只连接到我的服务器的HTML文件,但是我无法在服务器上运行单个测试。

How would I go about (using either http.Client or net.Stream ) to create a websocket client and have it interact with my server. 我将如何(使用http.Clientnet.Stream )创建一个websocket客户端并让它与我的服务器进行交互。

I'm targetting the (soon to be dead) draft 76 of the websocket spec. 我正在针对websocket规范的76(即将死)草案76。

The server side implementation I'm using is this 我正在使用的服务器端实现是这个

Since you already know that all current WebSocket version will be obsolete very soon and you're using a WebSocket server which supports the old 75 draft, it's fairly trivial to make one if you already have some server code lying around, so no need for the "security" header stuff in 76. 既然你已经知道所有当前的WebSocket版本很快就会过时而且你正在使用一个支持旧75草案的WebSocket服务器,如果你已经有一些服务器代码,那么制作一个很简单,所以不需要76年的“安全”标题。

Disclaimer: This thing had only 5 minute of testing or so but it should work for the most part. 免责声明:这件事只有5分钟的测试时间,但它应该在大多数情况下都有效。

Epic wall of code follows 史诗般的代码墙如下

var net = require('net');

function WebSocket(host, port, encoder, decoder) {
    this.encoder = encoder || function(data){return data.toString()};
    this.decoder = decoder || function(data){return data};
    this.socket = net.createConnection(port, host);

    this.connected = false;
    this.header = 0;
    this.bytesSend = 0;
    this.dataFrames = [];
    this.dataState = 0;

    var that = this;
    process.nextTick(function() {
        that.init(host, port);
    });
}


// Prototype -------------------------------------------------------------------
WebSocket.prototype = {
    onConnect: function() {console.log('connect');},
    onClose: function() {console.log('close');},
    onData: function(data) {console.log(data)},

    init: function(host, port) {
        var that = this;
        this.socket.addListener('connect', function() {
            var data ='GET / HTTP/1.1\r\n'
                  + 'Host: ' + host + ':' + port + '\r\n'
                  + 'Origin: websocket.node.js\r\n'
                  + 'Connection: Upgrade\r\n'
                  + 'Upgrade: WebSocket\r\n\r\n';

            that.socket.write(data, 'ascii');
            that.socket.flush();
        });
        this.socket.addListener('data', function(data) {that.read(data);});
        this.socket.addListener('end', function() {that.onClose();});
        this.socket.addListener('error', function(e) {console.log(e.message);that.close();});
    },

    send: function(data, encoded) {
        if (this.connected) {
            return this.write(encoded ? data : this.encoder(data));

        } else {
            return 0;
        }
    },

    close: function() {
        if (this.connected) {
            this.connected = false;
            this.write(null);
            this.socket.end();
            this.socket.destroy();
        }
    },

    read: function read(data) {
        for(var i = 0, l = data.length; i < l; i++) {
            var b = data[i];
            if (this.header < 4) {
                if ((this.header === 0 || this.header === 2) && b === 0x0d) {
                    this.header++;

                } else if ((this.header === 1 || this.header === 3) && b === 0x0a) {
                    this.header++;

                } else {
                    this.header = 0;
                }

                if (this.header === 4) {
                    this.connected = true;
                    this.onConnect();
                    this.header = 5;
                }

            } else {
                if (this.dataState === 0) {
                    this.dataState = b & 0x80 === 0x80 ? 2 : 1;

                // Low bit frame
                } else if (this.dataState === 1) {
                    if (b === 0xff) {
                        var buffer = new Buffer(this.dataFrames);
                        this.dataFrames = [];
                        this.dataState = 0;

                        if (!this.message(buffer.toString('utf8', 0, buffer.length))) {
                            this.send({error: 'Invalid Message.'});
                            this.close();
                            return;
                        }

                    } else {
                        this.dataFrames.push(b);
                    }

                // Unused high bit frames
                } else if (this.dataState === 2) {
                    if (b === 0x00) {
                        this.close();
                    }
                }
            }
        }
    },

    write: function(data) {
        var bytes = 0;
        if (!this.socket.writable) {
            return bytes;
        }

        try {
            this.socket.write('\x00', 'binary');
            if (typeof data === 'string') {
                this.socket.write(data, 'utf8');
                bytes += Buffer.byteLength(data);
            }
            this.socket.write('\xff', 'binary'); 
            this.socket.flush();
            bytes += 2;

        } catch(e) {}

        this.bytesSend += bytes;
        return bytes;
    },

    message: function(msg) {
        if (this.decoder) {
            try {
                msg = this.decoder(msg);

            } catch(e) {
                this.close();
                return false;
            }
        }
        this.onData(msg);
        return true;
    }
};

And here we set it up: 在这里我们设置它:

var bison = require('bison');

// automatically do some encoding/decoding magic
var test = new WebSocket('localhost', 28785, bison.encode, bison.decode);
test.onConnect = function() {

};

test.onData = function(data) {

};

Feel free to ask questions in the comments. 随意在评论中提问。

PS: For info on how it works, read the spec :P PS:有关其工作原理的信息,请阅读规范:P

Take a look at the modules page on the wiki , it has some modules for websocket clients available in npm . 看看wiki上的模块页面 ,它有一些用于在npm中可用的websocket客户端的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM