1.1.4 • Published 6 years ago

jquery-simple-websocket v1.1.4

Weekly downloads
15
License
(MIT OR Apache-2....
Repository
github
Last release
6 years ago

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

1.1.4

6 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago