apollographql / subscriptions-transport-ws

:arrows_clockwise: A WebSocket client + server for GraphQL subscriptions
https://www.npmjs.com/package/subscriptions-transport-ws
MIT License
1.52k stars 342 forks source link

How to use SubscriptionClient to get onclose code information #898

Closed XXuain closed 2 years ago

XXuain commented 3 years ago

Hi, I have a question is when I use new WebSocket(URL), I can find the CloseEvent and code or reason information in that CloseEvent. Like this:

export const connectSocket = () => {
  socket = new WebSocket(wsUrl);
  socket.onerror = function (err) {
    console.log('error >> ', err);
  };
  socket.onclose = function (e) {
    console.log('onclose >> ', e);
  };
};

截圖 2021-10-06 下午4 16 04

But, how can I use SubscriptionClient to find somthing like CloseEvent have code and reason information There is what I try.

import { SubscriptionClient } from 'subscriptions-transport-ws';
import { WebSocketLink } from '@apollo/client/link/ws';
export const wsClient = new SubscriptionClient(WS_URI, {
  reconnect: true
});
const wsLink = new WebSocketLink(wsClient);

wsClient.onError((error) => console.log('onError---', error));
wsClient.onConnected((info) => console.log('onConnected---', info));
wsClient.close((info) => console.log('onConnected---', info));
wsClient.onReconnected((info) => console.log('onReconnected---', info));
wsClient.onDisconnected((info) => console.log('onDisconnected---', info));

Above console only onError have display infomation, others display undefined 0.0