diff --git a/README.md b/README.md index 4514fc4..00c6e20 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,38 @@ fetch('/like/SOAPSAdaw', { +#### 调频广播 + +```javascript +let socket = new WebSocket("ws://localhost:2333"); + +socket.onopen = function(e) { + alert("[open] Connection established"); + alert("Sending to server"); + socket.send("My name is John"); +}; + +socket.onmessage = function(event) { + alert(`[message] Data received from server: ${event.data}`); +}; + +socket.onclose = function(event) { + if (event.wasClean) { + alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); + } else { + // e.g. server process killed or network down + // event.code is usually 1006 in this case + alert('[close] Connection died'); + } +}; + +socket.onerror = function(error) { + alert(`[error] ${error.message}`); +}; +``` + + + @@ -326,49 +358,5 @@ fetch('/like/SOAPSAdaw', { -查询示例: - -```javascript -fetch('/book').then(Response => Response.json()).then(data => { - console.log(data) -}) - -// 返回结果 -[ - {_id:'xxx', data:'xxxx'}, - {_id:'xxx', data:'xxxx'} -] -``` - - - -创建示例: - -```javascript -fetc('/book', { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - name: '创建一本新书', - data: '书的内容' - }), -}).then(Response => Response.json()).then(data => { - console.log(data) -}) - -// 返回结果 -{ - _id: 'xxx', - uid: 'xxx', - name: '创建一本新书', - data: '书的内容' -} -``` - - - #### 对象实体 diff --git a/index.js b/index.js index 1575d2f..60e4914 100644 --- a/index.js +++ b/index.js @@ -351,7 +351,7 @@ app.use(express.urlencoded({ extended: false })) app.use(session({ secret: 'kana', name: 'sid', resave: false, saveUninitialized: false, cookie: { maxAge: 180 * 24 * 3600000 }, store: session_store })) app.use('/data/file/', express.static('data/file')) -app.ws('/ws', websocketer) +app.ws('/', websocketer) app.route('/').get((req, res) => res.send(`

Hello World

`)) app.route('/user').post(object_create) app.route('/account').get(online, profile)