Skip to content

huazai2008101/nano-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nano client in typescript

Description

Nano typescript client SDK for Node.js and web browsers. This client SDK allows you to connect to Pomelo/Nano game servers easily. No external dependencies required for browser use, only requires 'ws' package for Node.js environment.

This project is modified from nano-client-ts, supporting both Node.js and web browser environments.

Installation

Using npm

npm install nano-client

Manual installation

Clone this repository and build it:

git clone <repository-url>
cd nano-client
npm install
npm run build

Usage

In Node.js

import { NanoClient } from 'nano-client';
// or using require
// const { NanoClient } = require('nano-client');

const client = new NanoClient();
client.init({
  host: 'example.com',
  port: 3010,
  reconnect: true
}, () => {
  console.log("Server connected...");
});

// Listen for events
client.on('someEvent', (data) => {
  console.log('Received event:', data);
});

// Make requests
client.request('someRoute', { param1: 'value1' }, (response) => {
  if (response.code === 200) {
    console.log('Success:', response);
  } else {
    console.log("Response error:", response);
  }
});

// Send notifications
client.notify('someRoute', { param1: 'value1' });

// Disconnect
client.disconnect();

In Browser

<script src="path/to/nano-client.js"></script>
<script>
  const client = new NanoClient.NanoClient();
  client.init({
    host: 'example.com',
    port: 3010,
    reconnect: true
  }, () => {
    console.log("Server connected...");
  });
  
  // Same API as Node.js version
</script>

Helper class example

You can create a helper class to simplify usage:

import { NanoClient } from 'nano-client';

export default class Server {
    private static client: NanoClient;
    
    public static init(host: string, port: number): void {
        Server.client = new NanoClient();
        Server.client.init({
            host: host,
            port: port,
            reconnect: true
        }, () => {
            console.log("Server connected...");
        });
    }
    
    public static listen(route: string, callback: Function): void {
        Server.client.on(route, (data: any) => {
            callback(data);
        });
    }

    public static request(route: string, msg: any, callback: Function): void {
        Server.client.request(route, msg, (response: any) => {
            if (response.code === 200) {
                callback(response);
            } else {
                console.log("Response error:", response);
            }
        });
    }
    
    public static notify(route: string, msg: any): void {
        Server.client.notify(route, msg);
    }
    
    public static disconnect(): void {
        Server.client.disconnect();
    }
}

Then use it like this:

// Initialize connection
Server.init('example.com', 3010);

// Make a request
Server.request("relation.list", {}, (response) => {
    console.log(response);
});

Features

  • Connect to Pomelo/Nano servers
  • Handshake authentication
  • Send request via JSON
  • Automatic reconnection
  • Heartbeat support
  • Cross-platform (Node.js and browsers)
  • TypeScript support

Issues

Any issues and PR's are welcomed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors