Giter Site home page Giter Site logo

Comments (14)

mkozjak avatar mkozjak commented on May 14, 2024 1

@ScriptWorker @davidmoshal @FMRb: This is now available in version 6.0.0-beta.0.

https://github.com/elpheria/rpc-websockets/blob/v6.x/API.md

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

Hello! Can you just run both server and client instances on both sides? That would solve the case.

from rpc-websockets.

ScriptWorker avatar ScriptWorker commented on May 14, 2024

Hello! Unfortunately no, as the server can't connect to the client. Basically we can't guarantee, that the client won't be under the firewall protection or even to have an external ip address.

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

@ScriptWorker: Ok, I agree and I would gladly merge the feature if no current use cases break!

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

@ScriptWorker Any progress here, maybe?

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

This will be available in version 5.x. Currently in development.

from rpc-websockets.

oxygen avatar oxygen commented on May 14, 2024

@ScriptWorker
https://www.npmjs.com/package/jsonrpc-bidirectional

from rpc-websockets.

FMRb avatar FMRb commented on May 14, 2024

Any update on version 5.x? I would like the library to support this feature

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

@FMRb Unfortunately, no. Out of resources currently!

from rpc-websockets.

davidmoshal avatar davidmoshal commented on May 14, 2024

you really ought to make it clear in the ReadMe that this is one-way, browser running procedures on a remote server only, because it's such a huge limitation of this library.

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

@FMRb: btw, any initial feedback on this feature, maybe? :)

from rpc-websockets.

mkozjak avatar mkozjak commented on May 14, 2024

Due to positive feedback this will hit master soon. It’s currently released as 6.x in beta due to insufficient migration docs, but that should be available soon. Closing this one.

from rpc-websockets.

huanshiwushuang avatar huanshiwushuang commented on May 14, 2024

My solution is that the client calls the server method, registers a function A, and then the server returns promise.
Then when calling A, make Promise resolve

example:

const res = {}

res.websocket = {
    makeUpServer(wsServer) {
        // client 注册方法
        wsServer.register('_calleeRegister', ({ methodName, namespace = '/', password = '' }) => {
            const registerPassword = password

            return new Promise((calleeResolve) => {
                wsServer.register(
                    methodName,
                    function (params) {
                        return new Promise((callerResolve, callerReject) => {
                            const { password } = params

                            // 密码校验不通过
                            if (registerPassword && registerPassword != password) {
                                return callerReject({
                                    state: 'fail',
                                    code: 'le3t8gjp',
                                    msg: `call password error`
                                })
                            }

                            // 删除密码
                            delete params.password

                            // 本次调用 id
                            const _callerCall = `${Date.now().toString(36)}-${Math.random()}`

                            // 转发 调用数据 给注册方
                            calleeResolve({
                                _callerCall,
                                params
                            })

                            // 缓存本次
                            _calleeReturn.value[_callerCall] = {
                                ms: Date.now(),
                                // 转发 返回数据 给调用方
                                callerResolve
                            }
                        })
                    },
                    namespace
                )
            })
        })
        // client 注册方法的返回
        const _calleeReturnCache = {
            // 清理超时的 callerResolve
            timeout: 1000 * 10,
            // 多久清理一次
            detectInterval: 1000 * 20,
            // 超时的 callerResolve
            value: {
                // _callerCall: {
                //     callerResolve: function () {},
                //     ms: Date.now(),
                // }
            }
        }
        // 定时检测超时的 _calleeReturnCache
        setInterval(() => {
            Object.keys(_calleeReturnCache.value).forEach((v) => {
                if (Date.now() - _calleeReturnCache.value[v].ms >= _calleeReturnCache.timeout) {
                    delete _calleeReturnCache.value[v]
                }
            })
        }, _calleeReturnCache.detectInterval)

        wsServer.register('_calleeReturn', ({ _callerCall, returnValue }) => {
            if (!_calleeReturnCache.value[_callerCall]) {
                return {
                    state: 'fail',
                    code: 'le3wr8e1',
                    msg: `not found _calleeReturnCache by _callerCall ${_callerCall}`
                }
            }
            _calleeReturnCache.value[_callerCall].callerResolve(returnValue)
            delete _calleeReturnCache.value[_callerCall]
            return 1
        })
    },
    makeUpClient(wsClient) {
        Object.assign(wsClient, {
            async register(methodName, handler, password = '', namespace = '/') {
                // 注册函数
                const start = async () => {
                    // 通知 Server 注册一个方法
                    const res = await wsClient.call(`_calleeRegister`, {
                        methodName,
                        namespace,
                        password
                    })
                    const { _callerCall, params } = res
                    let returnValue = handler(params)

                    if (/promise/i.test(returnValue.toString())) {
                        returnValue = await returnValue
                    }

                    wsClient.notify('_calleeReturn', {
                        _callerCall,
                        returnValue: returnValue || {}
                    })
                }
                await start()

                while (true) {
                    await start()
                }
            }
        })
    }
}

module.exports = res

from rpc-websockets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.