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

plain-websocket

v1.0.8

Published

- 本项目不能单独启动,也不能单独调试; - 本项目是为了plain-editor实现协同编辑,基于websocket进行简易封装的websocket库,目的在于制定可扩展的类型以及api快速实现客户端与服务端之间的websocket通信;

Downloads

16

Readme

plain-websocket

  • 本项目不能单独启动,也不能单独调试;
  • 本项目是为了plain-editor实现协同编辑,基于websocket进行简易封装的websocket库,目的在于制定可扩展的类型以及api快速实现客户端与服务端之间的websocket通信;

使用示例:

1、扩展客户端、服务端发送的消息类型

src/pws.ts

import 'plain-websocket/utils/pws.type';

/**
 * 扩展服务端以及客户端发送的消息类型
 * @author  韦胜健
 * @date    2023/10/17 18:32
 */
declare module 'plain-websocket/utils/pws.type' {

    /*自定义添加的服务端发送消息类型*/

    /*@formatter:off*/
    export interface iWssServerMessageExpander {serverUpdateArticle: any;}
    /*@formatter:on*/

    /*自定义添加的客户端发送消息类型*/

    /*@formatter:off*/
    export interface iWssClientMessageExpander {clientUpdatePause: { name: string, pause: boolean };}
    export interface iWssClientMessageExpander {clientUpdateArticle: any;}
    /*@formatter:on*/
}

export default {};

2、服务端使用示例

server/server.websocket.ts

import http from "http";
import {createServerPss} from "plain-websocket/server/createServerPss";
import '../src/pws';

/**
 * 初始化http server
 * @author  韦胜健
 * @date    2023/10/17 10:28
 */
const server = http.createServer(function (request, response) {
  wss.logInfo('Received request for ' + request.url);
  response.writeHead(404);
  response.end();
});

/**
 * 监听4477端口请求
 * @author  韦胜健
 * @date    2023/10/17 10:28
 */
server.listen(4477, function () {wss.logInfo('Server is listening on port 4477');});

const wss = createServerPss({
  /**
   * 基于http server实例初始化websocket
   * @author  韦胜健
   * @date    2023/10/17 10:28
   */
  wsParameters: {
    httpServer: server, // You should not use autoAcceptConnections for production
    // applications, as it defeats all standard cross-origin protection
    // facilities built into the protocol and the browser.  You should
    // *always* verify the connection's origin and decide whether or not
    // to accept it.
    autoAcceptConnections: false
  },
  /**
   * 判断来源是否可信,是则响应websocket连接,否则拒绝websocket连接
   * @author  韦胜健
   * @date    2023/10/17 10:29
   */
  originIsAllowed: (origin) => {
    // put logic here to detect whether the specified origin is allowed.
    return true;
  },
  messageHandler: {
    clientUpdatePause: (data, ConnectionHandler) => {
      const connectionMeta = ConnectionHandler.getConnections().find(i => i.name === data.name);
      if (!connectionMeta) {return;}
      connectionMeta.pause = data.pause;
      ConnectionHandler.updateConnection(connectionMeta);
    },
  }
});

3、客户端使用示例

import {designPage, onBeforeUnmount, reactive} from "plain-design-composition";
import {DisplayUsername} from "../components/DisplayUsername/DisplayUsername";
import {Table, Plc, Button} from 'plain-design';
import {addWindowListener, createClientPws} from "plain-websocket/client/createClientPws";
import {iWssClientMessageMeta, iWssServerUpdateConnMeta} from "plain-websocket/utils/pws.type";

export const TestWebSocket = designPage(() => {

    const state = reactive({
        connDataMetas: [] as iWssServerUpdateConnMeta[],
    });

    const wsc = createClientPws({
        port: 4477, messageHandler: {
            serverInit: () => {
                const clientMessageMeta: iWssClientMessageMeta = {type: 'clientInit', data: {listener: [{type: 'conn', data: {}}]}};
                wsc.send(clientMessageMeta);
            },
            serverUpdateConn: (data) => {
                state.connDataMetas = data;
            },
            serverUpdateArticle: () => {},
        }
    });

    onBeforeUnmount(
        addWindowListener('beforeunload', () => {wsc.ws.close();})
    );

    const handler = {
        /*切换链接暂停状态*/
        toggleConnectionPauseStatus: (connectionMeta: iWssServerUpdateConnMeta, pause?: boolean) => {
            const currentPause = state.connDataMetas.find(i => i.name === connectionMeta.name)!.pause;
            const clientMessageMeta: iWssClientMessageMeta = {type: "clientUpdatePause", data: {name: connectionMeta.name, pause: pause == undefined ? !currentPause : pause}};
            wsc.send(clientMessageMeta);
        },
    };

    return () => (
        !wsc.state.data?.name ? '连接中...' : (
            <div style={{padding: '0 16px'}}>
                <DisplayUsername name={wsc.state.data.name}/>
                <Table data={state.connDataMetas}>
                    <Plc title="用户名" field="name"/>
                    <Plc title="状态" field="pause" v-slots={{
                        normal: ({row}) => {
                            const meta: iWssServerUpdateConnMeta = row as any;
                            return !meta.pause ?
                                <Button size="small" label="暂停" status="primary" onClick={() => handler.toggleConnectionPauseStatus(meta)}/> :
                                <Button size="small" label="连接" status="secondary" onClick={() => handler.toggleConnectionPauseStatus(meta)}/>;
                        }
                    }}/>
                </Table>
            </div>
        )
    );
});

4、启动服务端的命令

"api": "ts-node --project node_modules/plain-websocket/server/server.tsconfig.json server/server.websocket.ts"