forked from mkaminsky11/codeyourcloud
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpackage.json
More file actions
38 lines (38 loc) · 6.82 KB
/
Copy pathpackage.json
File metadata and controls
38 lines (38 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
"name": "nodejs-websocket",
"version": "0.1.2",
"author": {
"name": "Sitegui",
"email": "sitegui@sitegui.com.br"
},
"description": "Basic server&client approach to websocket (text and binary frames)",
"main": "./index.js",
"repository": {
"type": "git",
"url": "https://github.com/sitegui/nodejs-websocket"
},
"keywords": [
"websocket",
"websocket-server",
"websocket-client"
],
"license": "GPL",
"engines": {
"node": ">=0.10"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"readme": "# Nodejs Websocket\r\nA nodejs module for websocket server and client\r\n\r\n# How to use it\r\nInstall with `npm install nodejs-websocket` or put all files in a folder called \"nodejs-websocket\", and:\r\n```javascript\r\nvar ws = require(\"nodejs-websocket\")\r\n\r\n// Scream server example: \"hi\" -> \"HI!!!\"\r\nvar server = ws.createServer(function (conn) {\r\n\tconsole.log(\"New connection\")\r\n\tconn.on(\"text\", function (str) {\r\n\t\tconsole.log(\"Received \"+str)\r\n\t\tconn.sendText(str.toUpperCase()+\"!!!\")\r\n\t})\r\n\tconn.on(\"close\", function (code, reason) {\r\n\t\tconsole.log(\"Connection closed\")\r\n\t})\r\n}).listen(8001)\r\n```\r\n\r\nSe other examples inside the folder samples\r\n\r\n# ws\r\nThe main object, returned by `require(\"nodejs-websocket\")`.\r\n\r\n## ws.createServer([options], [callback])\r\nReturns a new `Server` object.\r\n\r\nThe `options` is an optional object that will be handed to net.createServer() to create an ordinary socket.\r\nIf it has a property called \"secure\" with value `true`, tls.createServer() will be used instead.\r\n\r\nThe `callback` is a function which is automatically added to the `\"connection\"` event.\r\n\r\n## ws.connect(URL, [options], [callback])\r\nReturns a new `Connection` object, representing a websocket client connection\r\n\r\n`URL` is a string with the format \"ws://localhost:8000/chat\" (the port can be omitted)\r\n\r\n`options` is an object that will be passed to net.connect() (or tls.connect() if the protocol is \"wss:\").\r\nThe properties \"host\" and \"port\" will be read from the `URL`\r\n\r\n`callback` will be added as \"connect\" listener\r\n\r\n## ws.setBinaryFragmentation(bytes)\r\nSets the minimum size of a pack of binary data to send in a single frame (default: 512kiB)\r\n\r\n# Server\r\nThe class that represents a websocket server, much like a HTTP server\r\n\r\n## server.listen(port, [host], [callback])\r\nStarts accepting connections on a given `port` and `host`.\r\n\r\nIf the `host` is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).\r\n\r\nA `port` value of zero will assign a random port.\r\n\r\n`callback` will be added as an listener for the `'listening'` event.\r\n\r\n## server.socket\r\nThe underlying socket, returned by net.createServer or tls.createServer\r\n\r\n## server.connections\r\nAn Array with all connected clients. It's useful for broadcasting a message:\r\n```javascript\r\nfunction broadcast(server, msg) {\r\n\tserver.connections.forEach(function (conn) {\r\n\t\tconn.sendText(msg)\r\n\t})\r\n}\r\n```\r\n\r\n## Event: 'listening()'\r\nEmitted when the server has been bound after calling server.listen\r\n\r\n## Event: 'close()'\r\nEmitted when the server closes. Note that if connections exist, this event is not emitted until all connections are completely ended.\r\n\r\n## Event: 'error(errObj)'\r\nEmitted when an error occurs. The 'close' event will be called directly following this event.\r\n\r\n## Event: 'connection(conn)'\r\nEmitted when a new connection is made successfully (after the handshake have been completed). conn is an instance of Connection\r\n\r\n# Connection\r\nThe class that represents a connection, either a client-created (accepted by a nodejs ws server) or client connection.\r\nThe websocket protocol has two types of data frames: text and binary.\r\nText frames are implemented as simple send function and receive event.\r\nBinary frames are implemented as streams: when you receive binary data, you get a ReadableStream; to send binary data, you must ask for a WritableStream and write into it.\r\nThe binary data will be divided into frames and be sent over the socket.\r\n\r\nYou cannot send text data while sending binary data. If you try to do so, the connection will emit an \"error\" event\r\n\r\n## connection.sendText(str, [callback])\r\nSends a given string to the other side. You can't send text data in the middle of a binary transmission.\r\n\r\n`callback` will be added as a listener to write operation over the socket\r\n\r\n## connection.beginBinary()\r\nAsks the connection to begin transmitting binary data. Returns a WritableStream.\r\nThe binary transmission will end when the WritableStream finishes (like when you call .end on it)\r\n\r\n## connection.sendBinary(data, [callback])\r\nSends a single chunk of binary data (like calling connection.beginBinary().end(data))\r\n\r\n`callback` will be added as a listener to write operation over the socket\r\n\r\n## connection.close([code, [reason]])\r\nStarts the closing handshake (sends a close frame)\r\n\r\n## connection.socket\r\nThe underlying net or tls socket\r\n\r\n## connection.server\r\nIf the connection was accepted by a nodejs server, a reference to it will be saved here. null otherwise\r\n\r\n## connection.readyState\r\nOne of these constants, representing the current state of the connection. Only an open connection can be used to send/receive data.\r\n* connection.CONNECTING (waiting for handshake completion)\r\n* connection.OPEN\r\n* connection.CLOSING (waiting for the answer to a close frame)\r\n* connection.CLOSED\r\n\r\n## connection.outStream\r\nStores the OutStream object returned by connection.beginBinary(). null if there is no current binary data beeing sent.\r\n\r\n## connection.path\r\nFor a connection accepted by a server, it is a string representing the path to which the connection was made (example: \"/chat\"). null otherwise\r\n\r\n## Event: 'close(code, reason)'\r\nEmitted when the connection is closed by any side\r\n\r\n## Event: 'error(err)'\r\nEmitted in case of error (like trying to send text data while still sending binary data)\r\n\r\n## Event: 'text(str)'\r\nEmitted when a text is received. `str` is a string\r\n\r\n## Event: 'binary(inStream)'\r\nEmitted when the beginning of binary data is received. `inStream` is a ReadableStream\r\n\r\n## Event: 'connect()'\r\nEmitted when the connection is fully established (after the handshake)\r\n\r\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/sitegui/nodejs-websocket/issues"
},
"homepage": "https://github.com/sitegui/nodejs-websocket",
"_id": "nodejs-websocket@0.1.2",
"dist": {
"shasum": "89f30f7109588b6604a5594f644275832089daaf"
},
"_from": "nodejs-websocket@",
"_resolved": "https://registry.npmjs.org/nodejs-websocket/-/nodejs-websocket-0.1.2.tgz"
}