npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

j-spring-socket

v0.0.2

Published

封装了 socket 服务端的用法!

Downloads

2

Readme

j-spring-socket

封装了 socket 服务端的用法!

import { Component, spring } from 'j-spring';
import { Server, Socket } from 'socket.io';
import { Socket as cSocket, io } from 'socket.io-client';
import { Controller, Get, springWebModule } from 'j-spring-web';
import { socketModule, SocketHandler } from '../src/index';
import { emit } from 'process';

@Controller('testApi')
class TestApi {
  @Get()
  getMsg() {
    return { msg: 'hello world!' };
  }
}

const config = {
  'j-spring': {
    log: 'error',
  },
  'socket-io': {
    path: '/test-socket/',
  },
};

export interface ServerEvent {
  login(id: string, name: string): void;
  recive_user_msg(msg: string, cb: (n: number) => string): void;
}

export interface ClientEvent {
  recive_server_msg(msg: string): void;
}

export interface IoEvent {
  ping: () => void;
}

export type User = {
  id: string; //用户id
  name: string;
};

export type IoType = Server<ServerEvent, ClientEvent, IoEvent, User>;

export type SocketType = Socket<ServerEvent, ClientEvent, IoEvent, User>;

@Component()
class DiyHandler implements SocketHandler<SocketType, IoType> {
  getHandlerName(): string {
    return 'name:DiyHandler';
  }
  async registerEvent(socket: SocketType, io: IoType): Promise<void> {
    socket.on('login', (id, name) => {
      socket.data = { id, name };
      console.log(`用户登陆:${JSON.stringify(socket.data)}`);

      socket.emit('recive_server_msg', '你连接上服务器了,发送业务请求吧');
    });

    socket.on('recive_user_msg', (msg, d) => {
      console.log(`recive_user_msg => ${msg}`);
      d(100);
    });
  }
  isSocketHandler(): boolean {
    return true;
  }
  isAutoRegister(): boolean {
    return true;
  }
}

spring
  .loadConfig(config)
  .bindModule([springWebModule([TestApi]), socketModule([DiyHandler])])
  .invokeStarter()
  .then(() => {
    setTimeout(mainTest, 2000);
  });

const mainTest = async () => {
  console.log('开始测试!');

  // 设置服务器地址
  const socket: cSocket<ClientEvent, ServerEvent> = io('ws://127.0.0.1:3000', {
    path: '/test-socket/',
  }); // 假设你的Socket.IO服务器运行在 http://localhost:3000

  // 监听连接事件
  socket.on('connect', () => {
    console.log('Connected to the server!');

    // 可以在这里发送消息给服务器
    socket.emit('login', '1', 'xiaoming');
  });

  // 监听来自服务器的消息
  socket.on('recive_server_msg', (data: string) => {
    console.log('Received message from server:', data);
    socket.emit('recive_user_msg', '大数据请求', (d) => {
      console.log(`接受参数:${d}`);
      return d + '_client';
    });
  });

  // 监听断开连接事件
  socket.on('disconnect', () => {
    console.log('Disconnected from the server!');
  });

  socket.on('connect_error', (e) => {
    console.log(e);
  });
};