Simple, EventEmitter API for WebSockets (browser)
npm install simple-websocket
var SimpleWebsocket = require'simple-websocket'var socket = 'ws://echo.websocket.org'socketon'connect'// socket is connected!socketsend'sup!'socketon'data'console.log'got message: ' + data
Note: If you're NOT using browserify, then use the standalone
file included in this repo. This exports a
SimpleWebsocket function on the
Create a new WebSocket connection.
opts is specified, then it will be passed through to the underlying superclass,
Send text/binary data to the WebSocket server.
data can be any of several types:
Buffer (see buffer),
Blob (in browsers that support it).
Note: If this method is called before the
socket.on('connect') event has fired, then
data will be buffered.
Destroy and cleanup this websocket connection.
If the optional
onclose paramter is passed, then it will be registered as a listener on the 'close' event.
var Socket = require'simple-websocket'if SocketWEBSOCKET_SUPPORT// websocket support!else// fallback
Fired when the websocket connection is ready to use.
Received a message from the websocket server.
data will be either a
String or a
Buffer/Uint8Array (see buffer).
JSON strings will be parsed and the resulting
Called when the websocket connection has closed.
err is an
Fired when a fatal error occurs.
MIT. Copyright (c) Feross Aboukhadijeh.