# Nodejs Websocket
[![Build Status](https://travis-ci.org/sitegui/nodejs-websocket.svg?branch=master)](https://travis-ci.org/sitegui/nodejs-websocket)
[![Inline docs](https://inch-ci.org/github/sitegui/nodejs-websocket.svg?branch=master)](https://inch-ci.org/github/sitegui/nodejs-websocket)
[![Dependency Status](https://david-dm.org/sitegui/nodejs-websocket.svg)](https://david-dm.org/sitegui/nodejs-websocket)
A nodejs module for websocket server and client
# How to use it
Install with `npm install nodejs-websocket` or put all files in a folder called "nodejs-websocket", and:
```javascript
var ws = require("nodejs-websocket")
// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
console.log("New connection")
conn.on("text", function (str) {
console.log("Received "+str)
conn.sendText(str.toUpperCase()+"!!!")
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
}).listen(8001)
```
Se other examples inside the folder samples
# ws
The main object, returned by `require("nodejs-websocket")`.
## ws.createServer([options], [callback])
Returns a new `Server` object.
The `options` is an optional object that will be handed to net.createServer() to create an ordinary socket.
If it has a property called "secure" with value `true`, tls.createServer() will be used instead.
To support protocols, the `options` object may have either of these properties:
* `validProtocols`: an array of protocol names the server accepts. The server will pick the most preferred protocol in the client's list.
* `selectProtocol`: a callback to resolve the protocol negotiation. This callback will be passed two parameters: the connection handling the handshake and the array of protocol names informed by the client, ordered by preference. It should return the resolved protocol, or empty if there is no agreement.
The `callback` is a function which is automatically added to the `"connection"` event.
## ws.connect(URL, [options], [callback])
Returns a new `Connection` object, representing a websocket client connection
`URL` is a string with the format "ws://localhost:8000/chat" (the port can be omitted)
`options` is an object that will be passed to net.connect() (or tls.connect() if the protocol is "wss:").
The properties "host" and "port" will be read from the `URL`.
The optional property `extraHeaders` will be used to add more headers to the HTTP handshake request. If present, it must be an object, like `{'X-My-Header': 'value'}`.
The optional property `protocols` will be used in the handshake (as "Sec-WebSocket-Protocol" header) to allow the server to choose one of those values. If present, it must be an array of strings.
`callback` will be added as "connect" listener
## ws.setBinaryFragmentation(bytes)
Sets the minimum size of a pack of binary data to send in a single frame (default: 512kiB)
## ws.setMaxBufferLength(bytes)
Set the maximum size the internal Buffer can grow (default: 2MiB)
If at any time it stays bigger than this, the connection will be closed with code 1009
This is a security measure, to avoid memory attacks
# Server
The class that represents a websocket server, much like a HTTP server
## server.listen(port, [host], [callback])
Starts accepting connections on a given `port` and `host`.
If the `host` is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
A `port` value of zero will assign a random port.
`callback` will be added as an listener for the `'listening'` event.
## server.close([callback])
Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event. The optional callback will be called once the 'close' event occurs.
## server.socket
The underlying socket, returned by net.createServer or tls.createServer
## server.connections
An Array with all connected clients. It's useful for broadcasting a message:
```javascript
function broadcast(server, msg) {
server.connections.forEach(function (conn) {
conn.sendText(msg)
})
}
```
## Event: 'listening()'
Emitted when the server has been bound after calling server.listen
## Event: 'close()'
Emitted when the server closes. Note that if connections exist, this event is not emitted until all connections are completely ended.
## Event: 'error(errObj)'
Emitted when an error occurs. The 'close' event will be called directly following this event.
## Event: 'connection(conn)'
Emitted when a new connection is made successfully (after the handshake have been completed). conn is an instance of Connection
# Connection
The class that represents a connection, either a client-created (accepted by a nodejs ws server) or client connection.
The websocket protocol has two types of data frames: text and binary.
Text frames are implemented as simple send function and receive event.
Binary 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.
The binary data will be divided into frames and be sent over the socket.
You cannot send text data while sending binary data. If you try to do so, the connection will emit an "error" event
## connection.sendText(str, [callback])
Sends a given string to the other side. You can't send text data in the middle of a binary transmission.
`callback` will be added as a listener to write operation over the socket
## connection.beginBinary()
Asks the connection to begin transmitting binary data. Returns a WritableStream.
The binary transmission will end when the WritableStream finishes (like when you call .end on it)
## connection.sendBinary(data, [callback])
Sends a single chunk of binary data (like calling connection.beginBinary().end(data))
`callback` will be added as a listener to write operation over the socket
## connection.send(data, [callback])
Sends a given string or Buffer to the other side. This is simply an alias for `sendText()` if data is a string or `sendBinary()` if the data is a Buffer.
`callback` will be added as a listener to write operation over the socket
## connection.sendPing([data=''])
Sends a [ping](http://tools.ietf.org/html/rfc6455#section-5.5.2) with optional payload
## connection.close([code, [reason]])
Starts the closing handshake (sends a close frame)
## connection.socket
The underlying net or tls socket
## connection.server
If the connection was accepted by a nodejs server, a reference to it will be saved here. null otherwise
## connection.readyState
One of these constants, representing the current state of the connection. Only an open connection can be used to send/receive data.
* connection.CONNECTING (waiting for handshake completion)
* connection.OPEN
* connection.CLOSING (waiting for the answer to a close frame)
* connection.CLOSED
## connection.outStream
Stores the OutStream object returned by connection.beginBinary(). null if there is no current binary data beeing sent.
## connection.path
For a connection accepted by a server, it is a string representing the path to which the connection was made (example: "/chat"). null otherwise
## connection.headers
Read only map of header names and values. Header names are lower-cased
## connection.protocols
Array of protocols requested by the client. If no protocols were requested, it will be an empty array.
Additional resources on websocket subprotocols:
* [WebSocket Subprotocol Name Registry](http://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name)
* [The WebSocket Protocol](https://tools.ietf.org/html/rfc6455#section-11.3.4)
## connection.protocol
The protocol agreed for this connection, if any. It will be an element of `connection.protocols`.
## Event: 'close(code, reason)'
Emitted when the connection is closed by any side
## Event: 'error(err)'
Emitted in case of error (like trying to send text data while still
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于树莓派+Python+微信小程序打的物联网人工智能系统的设计与实现+详细文档+全部资料(高分毕业设计).zip基于树莓派+Python+微信小程序打的物联网人工智能系统的设计与实现+详细文档+全部资料(高分毕业设计).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 3、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于树莓派+Python+微信小程序打的物联网人工智能系统的设计与实现+详细文档+全部资料(高分毕业设计).zip (1027个子文件)
electron.asar 275KB
audioDevice 314B
audioExternal 378B
audioInside 456B
awake 61KB
v8_context_snapshot.bin 1.68MB
snapshot_blob.bin 1.38MB
natives_blob.bin 122KB
msc.cfg 686B
chirprc 2KB
wpa_supplicant.conf 72B
wpa_supplicant.conf 72B
create_ap 54KB
index.css 227KB
cascader.css 29KB
date-picker.css 28KB
transfer.css 27KB
col.css 25KB
pagination.css 23KB
table.css 22KB
message-box.css 21KB
time-picker.css 21KB
select.css 18KB
slider.css 18KB
tabs.css 16KB
base.css 16KB
dropdown.css 15KB
time-select.css 13KB
table-column.css 13KB
cascader-panel.css 13KB
tree.css 13KB
upload.css 12KB
icon.css 12KB
calendar.css 12KB
autocomplete.css 10KB
button.css 10KB
input-number.css 10KB
menu.css 9KB
index.css 9KB
demo.css 8KB
demo.css 8KB
demo.css 8KB
demo.css 8KB
color-picker.css 7KB
index.css 7KB
checkbox.css 7KB
input.css 7KB
normalize.css 6KB
step.css 5KB
collapse.css 5KB
drawer.css 5KB
tag.css 5KB
base.css 5KB
myweather_ico.css 5KB
weather_ico.css 5KB
iconfont.css 4KB
image.css 3KB
radio.css 3KB
form.css 3KB
dialog.css 3KB
select-dropdown.css 3KB
tooltip.css 3KB
carousel.css 2KB
voice.css 2KB
radio-button.css 2KB
iconfont.css 2KB
progress.css 2KB
popover.css 2KB
link.css 2KB
index.css 2KB
iconfont.css 2KB
message.css 2KB
alert.css 2KB
switch.css 2KB
notification.css 2KB
loading.css 2KB
popper.css 2KB
loading.css 2KB
loading.css 2KB
timeline-item.css 1KB
base.css 1KB
scrollbar.css 1KB
carousel-item.css 1024B
breadcrumb.css 1009B
display.css 982B
spinner.css 883B
row.css 870B
badge.css 831B
reset.css 811B
iconfont.css 720B
divider.css 695B
page-header.css 620B
rate.css 550B
avatar.css 547B
option.css 533B
weathern_ico.css 488B
myweathern_ico.css 488B
option-group.css 478B
card.css 463B
backtop.css 452B
共 1027 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
不走小道
- 粉丝: 3199
- 资源: 4969
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功