Skip to content

Commit e991dd3

Browse files
committed
dupi 添加websocket实时通信模块
2 parents 9002e31 + d1dd482 commit e991dd3

File tree

12 files changed

+302
-82
lines changed

12 files changed

+302
-82
lines changed

app/configs/router.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default () => (
1212
<Route path="/" component={base.app} onEnter={isLogin}>
1313
<IndexRoute component={base.example} />
1414
<Route path="/desk$/index" component={base.example} />
15+
<Route path="/socketReceive" component={base.socketReceive} />
1516
{/** *菜单 开始 */}
1617
<Route path="/echarts" component={menu.echarts} />
1718
<Route path="/editor" component={menu.editor} />

app/configs/socket.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import io from 'socket.io-client';
2+
// import queryString from 'query-string';
3+
4+
// const parsed = queryString.parse(window.location.search);
5+
6+
class Socket {
7+
constructor(ua = 'control', disable = false) {
8+
this.uid = this.random();
9+
// this.group = parsed.group || 'prod';
10+
this.disable = disable;
11+
this.socket = io('http://47.102.198.102:8080/', {
12+
transports: ['websocket'],
13+
query: {
14+
ua,
15+
group: 'prod',
16+
},
17+
});
18+
this.isSpeaker = false;
19+
this.speakerHandlers = [];
20+
21+
this.socket.on('room', ({ speaker }) => {
22+
this.isSpeaker = speaker === this.socket.id;
23+
this.speakerHandlers.forEach((cb) => {
24+
if (typeof cb === 'function') {
25+
cb(this.isSpeaker);
26+
}
27+
});
28+
});
29+
}
30+
31+
random = () => Date.now() + Math.random();
32+
33+
registerSpeakerHandler = (callback) => {
34+
this.unregisterSpeakerHandler(callback);
35+
this.speakerHandlers.push(callback);
36+
};
37+
38+
unregisterSpeakerHandler = (callback) => {
39+
let i = -1;
40+
this.speakerHandlers.some((cb, index) => {
41+
if (cb === callback) {
42+
i = index;
43+
return true;
44+
}
45+
return false;
46+
});
47+
if (i >= 0) {
48+
this.speakerHandlers.splice(i, 1);
49+
}
50+
};
51+
52+
dispatch = (data) => {
53+
// if (!this.isSpeaker && !data.speaker) {
54+
// return Promise.resolve();
55+
// }
56+
57+
data.uid = this.uid;
58+
data.timestamp = this.random();
59+
this.socket.emit('dispatch', data);
60+
if (data.speaker) {
61+
return new Promise((resolve, reject) => {
62+
this.registerSpeakerHandler((r) => {
63+
if (r) {
64+
resolve();
65+
} else {
66+
reject();
67+
}
68+
});
69+
});
70+
}
71+
return new Promise((resolve) => {
72+
const callback = (res) => {
73+
if (this.uid === res.uid && res.timestamp === data.timestamp) {
74+
this.socket.off('dispatch', callback);
75+
resolve();
76+
}
77+
};
78+
this.socket.on('dispatch', callback);
79+
});
80+
};
81+
82+
sent = (data, force) => {
83+
if (!this.isSpeaker && !force) {
84+
return;
85+
}
86+
data.uid = this.uid;
87+
data.group = this.group;
88+
this.socket.emit('msg', data);
89+
};
90+
91+
on = (name, call) => {
92+
if (!this.disable) {
93+
this.socket.on(name, call);
94+
}
95+
};
96+
97+
close = () => {
98+
this.socket.close();
99+
};
100+
101+
send = (data) => {
102+
this.dispatch({
103+
source: 'control',
104+
targets: ['control'],
105+
type: 'dispatch',
106+
payload: data,
107+
})
108+
}
109+
}
110+
111+
const socket = new Socket('control', window.location.pathname === '/console');
112+
export const MsgSocket = Socket;
113+
114+
export default socket;

app/mocks/apis/base/menu.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ module.exports = {
99
resKey: 'desk$/index',
1010
resIcon: 'pgmb',
1111
},
12+
{
13+
id: 10064,
14+
resName: 'socket接收',
15+
resKey: 'socketReceive',
16+
resIcon: 'pgmb',
17+
},
1218
{
1319
id: 600110233,
1420
resName: '图表',

app/pages/base/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import '@styles/base.less'
1212
import Header from './app/header'
1313
import LeftNav from './app/leftNav'
1414
// import TabList from './app/tabList'
15-
15+
import SocketComponent from './socket'
1616

1717
@connect((state, props) => ({}))
1818
export default class App extends Component {
@@ -188,6 +188,7 @@ export default class App extends Component {
188188
return (
189189
<LocaleProvider locale={zhCN}>
190190
<div id="container">
191+
<SocketComponent />
191192
{
192193
idRenderChild && !isIframe ? <Header
193194
gMenuList={gMenuList}

app/pages/base/example.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React, { Component } from 'react'
22
// import PropTypes from 'prop-types'
3-
// import { } from 'antd'
3+
import { Button } from 'antd'
44
// import {connect} from 'react-redux'
55
// import {} from '@actions/xxx'
6+
import Socket from '@configs/socket'
67

78
// @connect((storeState)=>({}))
89

910
export default class app extends Component {
10-
static defaultProps={
11+
static defaultProps = {
1112
}
1213

1314
static propTypes = {
@@ -18,17 +19,25 @@ export default class app extends Component {
1819
this.state = {}
1920
}
2021

21-
componentDidMount() {}
22+
componentDidMount() { }
2223

2324
// #region vscode 1.17的收缩代码块功能 业务代码
2425

2526

2627
// #endregion
2728

29+
// 发送socket数据
30+
onClickSend = () => {
31+
Socket.send({ type: 'receive/hello3', data: { name: 'dupi' } })
32+
}
33+
2834
render() {
2935
return (
3036
<div className="page">
3137
示范页面
38+
<div>
39+
<Button onClick={this.onClickSend}>发送</Button>
40+
</div>
3241
</div>
3342
)
3443
}

app/pages/base/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import example from './example'
55
import login from './login'
66
import notfound from './notfound'
77
import app from './app'
8+
import socketReceive from './socketReceive'
89

9-
export { developing, example, login, notfound, app }
10+
export { developing, example, socketReceive, login, notfound, app }

app/pages/base/socket.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: hy
3+
* @Date: 2019-05-24 14:46:23
4+
* @Last Modified by: hy
5+
* @Last Modified time: 2019-05-24 15:05:01
6+
*/
7+
8+
// socket on
9+
10+
import React, { Component } from 'react'
11+
import socket from '@configs/socket'
12+
import { socketReceive } from '@actions/common'
13+
import { connect } from 'react-redux'
14+
15+
@connect(() => ({}))
16+
export default class SocketOn extends Component {
17+
componentDidMount() {
18+
console.log('socket didmount')
19+
this.init()
20+
}
21+
22+
init = () => {
23+
const callback = (res) => {
24+
this.props.dispatch(socketReceive(res))
25+
};
26+
socket.on('dispatch', callback);
27+
}
28+
29+
render() {
30+
return null
31+
}
32+
}

app/pages/base/socketReceive.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { Component } from 'react'
2+
// import PropTypes from 'prop-types'
3+
import { Button } from 'antd'
4+
import { connect } from 'react-redux'
5+
// import {} from '@actions/xxx'
6+
import Socket from '@configs/socket'
7+
8+
@connect(store => ({ socketCollection: store.socketCollection }))
9+
10+
export default class app extends Component {
11+
static defaultProps = {
12+
}
13+
14+
static propTypes = {
15+
}
16+
17+
constructor(props) {
18+
super(props)
19+
this.state = {}
20+
}
21+
22+
componentDidMount() { }
23+
24+
// #region vscode 1.17的收缩代码块功能 业务代码
25+
26+
27+
// #endregion
28+
29+
// 发送socket数据
30+
onClickSend = () => {
31+
Socket.dispatch({ type: 'receive/hello2' })
32+
}
33+
34+
render() {
35+
const { socketCollection = {} } = this.props
36+
return (
37+
<div className="page">
38+
socket receive 页面示例
39+
<div>
40+
<h5>收到数据:</h5>
41+
<pre>
42+
<code>
43+
{JSON.stringify(socketCollection, null, 2)}
44+
</code>
45+
</pre>
46+
</div>
47+
</div>
48+
)
49+
}
50+
}

app/redux/actions/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ export const login = createAjaxAction(common.login, requestLogin, recevieLogin)
1212
// gFormCache gfor2.0m的缓存
1313
export const setGformCache2 = createAction('set gform cache2')
1414
export const clearGformCache2 = createAction('clear gform cache2')
15+
16+
// socket receive
17+
export const socketReceive = createAction('socketReceive')

app/redux/reducers/common.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { handleActions } from 'redux-actions'
22

33
// 登陆返回结果
4-
const loginState = () => ({ })
4+
const loginState = () => ({})
55
export const loginResponse = handleActions({
66
'request login'(state, action) {
77
return { ...state, loading: true }
@@ -48,3 +48,13 @@ export const allRetrievalResult = handleActions({
4848
return { ...res.data, loading: false }
4949
},
5050
}, allRetrievalState)
51+
52+
53+
// socket相关操作
54+
export const socketCollection = handleActions({
55+
'socketReceive'(state, action) {
56+
// eslint-disable-next-line no-unused-vars
57+
const data = action.payload
58+
return { data }
59+
},
60+
}, {})

0 commit comments

Comments
 (0)