jQuery Simple WebSocket - a fluent, gracefull websocket
Pull request Compare This branch is 90 commits ahead, 1 commit behind dchelimsky:master.
Jannis Bloemendal
Jannis Bloemendal resized logo
Latest commit 3b599ac Oct 28, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist Message types #20 Dec 29, 2017
example Example dependency management #18 Apr 5, 2017
src Message types #20 Dec 29, 2017
test Message types #20 Dec 29, 2017
.gitignore Example dependency management #18 Apr 5, 2017
.travis.yml Message types #20 Dec 29, 2017
Gruntfile.js Message types #20 Dec 29, 2017
LICENSE.txt Update LICENSE.txt Feb 2, 2016
NOTICE.txt - adding jasmine tests Jan 31, 2016
README.md travis #10 Mar 29, 2017
blogger.png Adding blogger badge Mar 18, 2017
bower.json Message types #20 Dec 29, 2017
package.json Message types #20 Dec 29, 2017
websocket.png resized logo Oct 28, 2018

README.md

blogger article npm version travis

jQuery Simple WebSocket

jQuery Simple WebSocket

Send and receive data through a fluent deferred interface, handling connections gracefully.

Example

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
<script type="text/javascript" src="jquery.simple.websocket.js"></script>
<script type="text/javascript">
    var webSocket = $.simpleWebSocket({ url: 'ws://127.0.0.1:3000/' });
    
    // reconnected listening
    webSocket.listen(function(message) {
        console.log(message.text);
    });

    webSocket.send({ 'text': 'hello' }).done(function() {
        // message send
    }).fail(function(e) {
        // error sending
    });
</script>

fluent:

var webSocket = $.simpleWebSocket({ url: 'ws://127.0.0.1:3000/' })
.listen(function(message) { console.log('listener1: '+message.text); })
.listen(function(message) { console.log('listener2: '+message.text); })
.listen(function(message) { console.log('listener3: '+message.text); })
.send({'text': 'hello'});

Usage

var socket = $.simpleWebSocket(
    {
        url: 'ws://127.0.0.1:3000/',
        protocols: 'your_protocol', // optional
        timeout: 20000, // optional, default timeout between connection attempts
        attempts: 60, // optional, default attempts until closing connection
        dataType: 'json' // optional (xml, json, text), default json
    }
);

socket.connect();

socket.isConnected(); // or: socket.isConnected(function(connected) {});

socket.send({'foo': 'bar'});

socket.listen(function(data) {});

socket.remove(listenerCallback);

socket.removeAll();

socket.close();

Web Chat Example

  • start nodejs websocket server:
$ node src/server.js
  • open example/example.html

History