V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xjdata
V2EX  ›  问与答

基于 dva,websocket,model,subscriptions,service 求个例子/资料. 不好意思做伸手党了.

  •  
  •   xjdata · 2017-02-07 22:07:59 +08:00 · 3529 次点击
    这是一个创建于 2649 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想请大神 基于 dva,websocket, model,subscriptions, service.

    给一个能够接受服务器消息,向服务器发送消息,主动断开,可处理异常断开的例子.

    折腾 6 个小时,实在没办法,不好意思做伸手党了. 实在是水平太菜,也不知道该搜什么关键词了.

    谢谢.

    这是 model

    import * as service from '../services/websocket'
    
    export default {
        namespace: 'websocket',
        state: {
            messages: undefined,
            client_id: undefined,
        },
        subscriptions: {
            watchWebSocket({dispatch, history}) {
                return history.listen(({pathname}) => {
                    dispatch({type: 'open'});
                });
            }
        },
        effects: {
            * open({payload}, {put, call}) {
                //wss://echo.websocket.org
                const config = {url: 'wss://echo.websocket.org', user_name: 'xxx', user_id: 1, room_id: 999};
                // service.watchList(config, (data) => {
                //     dispatch({type: data.type, payload: data});
                // });
                const {data} = yield call(service.watchList, config);
                console.log('result', data);
            },
            * message({payload}, {put, call}) {
                console.log('message', payload);
                yield put({type: 'messageSuccess', payload: payload.client_id});
            },
            * send({payload}, {put, call}) {
                yield call(service.send, {config: {url: 'wss://echo.websocket.org'}, data: payload});
            },
        },
    
        reducers: {
            openSuccess(state, action) {
                //client_id:1
                return {...state, ... action.payload}
            },
            messageSuccess(state, action) {
            //messages{type:'message',data:{....}}
                return {...state, ... action.payload}
            },
        },
    }
    
    

    这是 service

    let websocket = undefined;
    function getWebsocket(url) {
        console.log('websocket client', websocket);
        if (!websocket) {
            websocket = new WebSocket(url);
        }
        return websocket;
    }
    
    export async function watchList(config, cb) {
        const client = getWebsocket(config.url);
        client.onopen = () => {
            client.send(JSON.stringify({type: 'login', ...config}));
        };
        // return client.onmessage = (data) => {
        //     cb(data);
        // };
    
        client.onmessage = (evt) => {
            const data = JSON.parse(evt.data);
            if (data) {
                switch (data.type) {
                    case 'ping':
                        client.send('{"type":"pong"}');
                        // cb(data);
                        break;
                    case 'login':
                    case 'message':
                        // cb(data);
                        new Promise(function (resolve, reject) {
                            return resolve({data});
                        });
                        break;
                    // 用户退出 更新用户列表
                    case 'logout':
                        break;
                }
            }
        };
    }
    
    
    export async function send(config, data) {
        console.log('send', data);
        const websocket = getWebsocket(config.url);
        websocket.send(JSON.stringify(data));
    }
    
    export async function logout(config, code, reason) {
        const websocket = getWebsocket(config.url);
        websocket.close(code, reason);
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2615 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:52 · PVG 23:52 · LAX 08:52 · JFK 11:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.