0.1.6 • Published 9 years ago

erlws v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 years ago

erlws: a node.js websocket library

Installing

npm install --save erlws

Usage

It is pretty similar to original ws. The receive is used to allow us to wait for particular messages.

Each socket has its own input queue for messages it receives. New messages received are put at the end of the queue. When receive is executed, the first message in the queue is matched against the first pattern in the receive. If this matches, the message is removed from the queue and the actions corresponding to the pattern are executed.

However, if the first pattern does not match, the second pattern is tested. If this matches, the message is removed from the queue and the actions corresponding to the second pattern are executed. If the second pattern does not match, the third is tried and so on until there are no more patterns to test. If there are no more patterns to test, the first message is kept in the queue and the second message is tried instead. If this matches any pattern, the appropriate actions are executed and the second message is removed from the queue (keeping the first message and any other messages in the queue). If the second message does not match, the third message is tried, and so on, until the end of the queue is reached. If the end of the queue is reached, the socket waits until a new message is received and this procedure is repeated.

Example

var ws = require('erlws');
var wss = new ws.Server({port: 3000});

wss.on('connection', function (socket) {
  console.log('connected');

  socket.on('close', function () {
    console.log('disconnected');
  });

  var state = function (done) {
    socket.receive([{
      pattern: {
        // specify key: value which should be matched
        // empty pattern matches all messages
      },
      action: function (message) {
        console.log(message);
        // new state specified
        // it is not changed in this example
        done(state);
      }
    }, {
      timeout: 1000,
      action: function () {
        console.log('timeout');
        done(state);
      }
    }]);
  };

  (function run(state) {
    state(function (newstate) {
      run(newstate);
    });
  })(state);
});

License

(The MIT License)

Copyright (c) 2016 Boris Molodenkov <boris.molodenkov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago