Code Monkey home page Code Monkey logo

deno-websocket's Introduction

logo

deno websocket

deno doc GitHub nest badge

๐Ÿฆ• A simple WebSocket library like ws of node.js library for deno

This library is wrapping the ws standard library as a server-side and the native WebSocket API as a client-side. You can receive callbacks at the EventEmitter and can use the same object format on both the server-side and the client-side.

  • Deno >= 1.8.3

Quick Start

Example Demo

demo

Server side

$ deno run --allow-net https://deno.land/x/[email protected]/example/server.ts

Client side

$ deno run --allow-net https://deno.land/x/[email protected]/example/client.ts
ws connected! (type 'close' to quit)
> something

Usage

Server side

import { WebSocketClient, WebSocketServer } from "https://deno.land/x/[email protected]/mod.ts";

const wss = new WebSocketServer(8080);
wss.on("connection", function (ws: WebSocketClient) {
  ws.on("message", function (message: string) {
    console.log(message);
    ws.send(message);
  });
});

Client side

import { WebSocketClient, StandardWebSocketClient } from "https://deno.land/x/[email protected]/mod.ts";
const endpoint = "ws://127.0.0.1:8080";
const ws: WebSocketClient = new StandardWebSocketClient(endpoint);
ws.on("open", function() {
  console.log("ws connected!");
  ws.send("something");
});
ws.on("message", function (message: string) {
  console.log(message);
});

Documentation

WebSocketServer

Event

event detail
connection Emitted when the handshake is complete
error Emitted when an error occurs

Field

field detail type
server.clients A set that stores all connected clients Set<WebSocket>

Method

method detail
close() Close the server

WebSocketClient

Event

event detail
open Emitted when the connection is established
close Emitted when the connection is closed
message Emitted when a message is received from the server
ping Emitted when a ping is received from the server
pong Emitted when a pong is received from the server
error Emitted when an error occurs

Field

field detail type
websocket.isClose Get the close flag Boolean | undefined

Method

method detail
send(message:string | Unit8Array) Send a message
ping(message:string | Unit8Array) Send the ping
close([code:int[, reason:string]]) Close the connection with the server
forceClose() Forcibly close the connection with the server

LICENSE

MIT LICENSE

deno-websocket's People

Contributors

asjur avatar hundeklemmen avatar ryo-ma avatar saabi avatar tommardh avatar userjhansen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.