async-call-rpc icon indicating copy to clipboard operation
async-call-rpc copied to clipboard

Failed to connect to server

Open Baker68 opened this issue 2 years ago • 1 comments

Server code :

const { WebSocketChannel } = require('async-call-rpc/utils/node/websocket.server')
const { Msgpack_Serialization } = require('async-call-rpc/utils/node/msgpack')
const { AsyncCall } = require('async-call-rpc')
const { Server } = require('ws')
const {compute}  = require('./methods');

const ws = new Server({ port: 3456 })
const channel = new WebSocketChannel(ws)

AsyncCall(compute,  { channel: channel, serializer: Msgpack_Serialization() })

Client code :

const {WebSocketChannel} = require('async-call-rpc/utils/node/websocket.server')
const {Msgpack_Serialization} = require('async-call-rpc/utils/node/msgpack')
const {AsyncCall} = require('async-call-rpc')
const {WebSocket} = require('ws');
const {compute} = require("./methods");

const socket = new WebSocket('ws://localhost:3456/');
const channel = new WebSocketChannel(socket)


const server = AsyncCall(compute, {channel: channel, serializer: Msgpack_Serialization()});

server.compute('a')

methods.js :

function compute(data) {
    return data;
}

module.exports = {compute}

it throws :

(node:27940) UnhandledPromiseRejectionWarning: TypeError: channel.send is not a function
    at sendPayload (/home/crooked/Desktop/work/async-call-rpc/node_modules/async-call-rpc/out/base.js:655:28)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:27940) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:27940) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

what am I doing wrong ? thanks in advance.

Baker68 avatar Mar 08 '22 09:03 Baker68

hello! thanks for your interest in using this library. I have to say I didn't document this library well, I'm sorry for that.

I want to point out that:

  1. The first parameter should be an object that contains your function. You only need to provide that on the server. For the client-side, you can pass an empty object.
AsyncCall(method, ...) // wrong
AsyncCall({ method }) // correct
  1. I didn't provide a WebSocket client channel for Node.js. I only provide a WS client channel for the browser. You should use the browser to connect to your WS server.

If the built-in library doesn't work well for you, you can always implement your own channel to communicate with the server and the client.

Thanks!

Jack-Works avatar Mar 08 '22 14:03 Jack-Works