Skip to content

🦕 A simple WebSocket library like ws of node.js library for deno

License

Notifications You must be signed in to change notification settings

asjur/deno-websocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

deno websocket

deno doc GitHub nest badge

🦕 A simple WebSocket library like ws of node.js library for deno

  • Deno >= 1.2

⚠️ I will deprecate the implementation of WebSocket client in the next version. But I will continue developing WebSocket Server.
Please use the following API for WebSocket client
https://deno.land/posts/v1.4#websocket-api

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 { WebSocket, WebSocketServer } from "https://deno.land/x/[email protected]/mod.ts";

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

Client side

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

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

WebSocket

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

About

🦕 A simple WebSocket library like ws of node.js library for deno

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%