Skip to content
This repository was archived by the owner on Mar 21, 2020. It is now read-only.

Commit bef765e

Browse files
committed
basic functions finished
1 parent 950e6e2 commit bef765e

34 files changed

+4272
-302
lines changed

.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"es2015",
5+
{
6+
"modules": false
7+
}
8+
]
9+
],
10+
"plugins": ["external-helpers"]
11+
}

.eslintrc.yml

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
# Support ES2016 features
2-
# extends: airbnb
3-
#
4-
# parser: babel-eslint
2+
extends: airbnb
3+
4+
parser: babel-eslint
5+
6+
# plugins: [
7+
# "babel"
8+
# ]
59

610
parserOptions:
711
sourceType: module
812

913
rules:
1014
strict: 0
1115
arrow-parens: 0
16+
# babel/arrow-parens: 1
17+
# babel/generator-star-spacing: 1
18+
# babel/new-cap: 1
19+
# babel/array-bracket-spacing: 1
20+
# babel/object-curly-spacing: [1, "always"]
21+
# babel/object-shorthand: 1
22+
# babel/no-await-in-loop: 1
23+
# babel/flow-object-type: 1
1224
no-console: 0
1325
spaced-comment: [2, "always", { exceptions: ["-"]}]
1426
eqeqeq: 0
@@ -19,17 +31,21 @@ rules:
1931
yoda: 0
2032
arrow-spacing: 2
2133
dot-location: [2, "property"]
34+
prefer-arrow-callback: 2
2235
keyword-spacing: [2, { after: true, before: true }]
2336
indent: [2, 2]
2437
camelcase: 0
2538
key-spacing: [1, { beforeColon: false, afterColon: true, mode: "minimum" }]
2639
space-in-parens: [1, "never"]
2740
arrow-body-style: [1, "as-needed"]
2841
no-param-reassign: 0
29-
new-cap: [2, {"capIsNewExceptions": ["Immutable.Map", "Immutable.Set", "Immutable.List", "App", "Page"]}]
42+
no-unused-vars: [1, {"vars": "local", "args": "none"}]
43+
no-unused-expressions: ["error", { "allowShortCircuit": true }]
44+
no-restricted-syntax: 0
45+
new-cap: ["error", { "capIsNew": false }]
46+
# new-cap: [2, {"capIsNewExceptions": ["Immutable.Map", "Immutable.Set", "Immutable.List"]}]
3047
no-underscore-dangle: 0
3148
no-use-before-define: 0
32-
prefer-arrow-callback: [1, { allowNamedFunctions: true}]
33-
space-before-blocks: 0
34-
max-len: 0
35-
no-unused-vars: 0
49+
func-names: 0
50+
no-cond-assign: 0
51+
default-case: 0

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
# weapp-socket-io
22

3-
微信小程序的SocketIO实现,基于[CFETeam的实现](https://github.com/CFETeam/weapp-demo-websocket/blob/master/app/lib/wxsocket.io.js)基础上完善
3+
微信小程序的 Socket.io client 实现,**压缩之后体积为 16K**
44

5-
# DEMO
5+
> 为了让开发者已最低的成本上手,本类库封装了与 socket.io 一致的 API 供开发者调用。
66
7-
[项目附了一个微信小程序的Demo项目](https://github.com/fanweixiao/wxapp-socket-io/tree/master/demo)演示了[接入Socket.io官方的演示聊天室](http://socket.io/demos/chat/),在运行项目Demo时方便测试。
7+
# Features
8+
目前已支持
9+
* NameSpace
10+
* Singleton
11+
* Reconnect
12+
13+
# Build
14+
`npm run build`
815

9-
关于在微信小程序中如何使用Socket.io可以[参考腾讯云前端团队的文档](https://github.com/CFETeam/weapp-demo-websocket/blob/master/README.md)
16+
生产环境可使用 `NODE_ENV=production npm run build` 进行压缩编译
1017

1118
# How to use
19+
拷贝 build 目录的 index.js 文件到你的项目目录,并 require
1220

13-
```javascript
14-
const opts = {}
15-
const socket = this.globalData.socket = new WxSocketIO()
16-
socket.connect('ws://chat.socket.io', opts)
17-
.then(_ => {
18-
console.info('App::WxSocketIO::onOpen')
19-
console.info('App:onShow:', that.globalData)
20-
})
21-
.catch(err => {
22-
console.error('App::WxSocketIO::onError', err)
23-
})
24-
```
21+
`const io = require('yourPath/build/index.js')`
2522

26-
其中`socket.connect(ws_url, opts)`中,`opts`目前可选值是`path`,用来指定使用socket.io时默认的path,比如设置opts为下列值:
23+
为了
24+
使用风格与 socket.io 完全一致
2725

2826
```javascript
29-
{
30-
query: 'fanweixiao',
31-
with: 'mia&una',
32-
}
33-
```
34-
将会设置`Socket.io`的url为`ws://chat.socket.io/fanweixiao/?EIO=3&transport=websocket&with=mia%26una`
27+
const news = io('ws://localhost:9999/news')
28+
news.on('news', function(data) {
29+
console.log('======news data: ', data)
30+
news.emit('old', '1234abc')
31+
})
3532

33+
const chat = io('ws://localhost:9999/chat')
34+
console.log('chat: ', chat)
35+
chat.on('chat', function(data) {
36+
console.log('======chat data: ', data)
37+
chat.emit('comment', 'Hi server chat')
38+
})
39+
```
3640
# Collaborator
3741

3842
+ [gongzili](https://github.com/gongzili456)
3943
+ [C.C.](https://github.com/fanweixiao)
40-
44+
4145
# TODO
42-
43-
+ Connect 单例
4446
+ emit buffer
45-
+ 重连机制
4647
+ Room的支持
47-
+ Namespace的支持

build/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/app.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

demo/app.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

demo/lib/emitter.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/lib/serialize.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/lib/wxsocket.io.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/pages/index.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

demo/pages/index.wxml

Lines changed: 0 additions & 5 deletions
This file was deleted.

demo/pages/index.wxss

Lines changed: 0 additions & 12 deletions
This file was deleted.

emitter.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

package.json

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "wxapp-socket-io",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "wxsocket.io.js",
5+
"main": "src/index.js",
66
"scripts": {
7-
"eslint": "eslint ."
7+
"build": "./node_modules/.bin/rollup -c"
88
},
99
"repository": {
1010
"type": "git",
@@ -16,14 +16,44 @@
1616
"url": "https://github.com/fanweixiao/wxapp-socket-io/issues"
1717
},
1818
"homepage": "https://github.com/fanweixiao/wxapp-socket-io#readme",
19-
"dependencies": {},
19+
"dependencies": {
20+
"backo2": "^1.0.2",
21+
"component-bind": "^1.0.0",
22+
"component-emitter": "^1.2.1",
23+
"indexof": "^0.0.1",
24+
"parsejson": "0.0.2",
25+
"parseuri": "0.0.4"
26+
},
2027
"devDependencies": {
21-
"babel-eslint": "^7.0.0",
22-
"eslint": "^3.5.0",
23-
"eslint-config-standard": "^6.0.1",
24-
"eslint-plugin-import": "^1.15.0",
25-
"eslint-plugin-promise": "^2.0.1",
26-
"eslint-plugin-standard": "^2.0.0"
28+
"babel-cli": "^6.10.1",
29+
"babel-core": "^6.10.4",
30+
"babel-eslint": "^6.1.0",
31+
"babel-plugin-add-module-exports": "^0.2.1",
32+
"babel-plugin-external-helpers": "^6.8.0",
33+
"babel-plugin-syntax-async-functions": "^6.8.0",
34+
"babel-plugin-transform-runtime": "^6.9.0",
35+
"babel-preset-es2015": "^6.16.0",
36+
"babel-preset-es2015-rollup": "^1.2.0",
37+
"babel-preset-eslatest-node6": "^1.0.1",
38+
"babel-register": "^6.9.0",
39+
"debug": "^2.2.0",
40+
"eslint": "^3.0.0",
41+
"eslint-config-airbnb": "^9.0.1",
42+
"eslint-config-standard": "^5.3.1",
43+
"eslint-plugin-babel": "^3.3.0",
44+
"eslint-plugin-import": "^1.10.2",
45+
"eslint-plugin-jsx-a11y": "^1.5.3",
46+
"eslint-plugin-promise": "^1.3.2",
47+
"eslint-plugin-react": "^5.2.2",
48+
"eslint-plugin-standard": "^1.3.2",
49+
"nodemon": "^1.9.2",
50+
"rollup": "^0.36.3",
51+
"rollup-plugin-babel": "^2.6.1",
52+
"rollup-plugin-commonjs": "^5.0.4",
53+
"rollup-plugin-eslint": "^3.0.0",
54+
"rollup-plugin-node-resolve": "^2.0.0",
55+
"rollup-plugin-replace": "^1.1.1",
56+
"rollup-plugin-uglify": "^1.0.1"
2757
},
2858
"engines": {
2959
"node": ">=6"

rollup.config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import babel from 'rollup-plugin-babel'
2+
import resolve from 'rollup-plugin-node-resolve'
3+
import commonjs from 'rollup-plugin-commonjs'
4+
import replace from 'rollup-plugin-replace'
5+
import uglify from 'rollup-plugin-uglify'
6+
7+
export default {
8+
entry: 'src/index.js',
9+
dest: 'build/index.js',
10+
format: 'cjs',
11+
sourceMap: false,
12+
plugins: [
13+
resolve({
14+
jsnext: true,
15+
main: true,
16+
browser: true,
17+
}),
18+
commonjs(),
19+
babel({
20+
presets: ['es2015-rollup'],
21+
runtimeHelpers: true,
22+
exclude: 'node_modules/**',
23+
}),
24+
replace({
25+
ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
26+
}),
27+
(process.env.NODE_ENV === 'production' && uglify()),
28+
],
29+
}

serialize.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/debug.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default nsp => {
2+
return (...log) => {
3+
if (__wxConfig.debug) {
4+
console.log.call(null, nsp, ...log)
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)