TCP socket library for Deno
import { Server } from "https://deno.land/x/[email protected]/mods.ts";
// or
import { Client } from "https://deno.land/x/[email protected]/mods.ts";
import { Server } from "https://deno.land/x/[email protected]/mods.ts";
const server = new Server({ port: 8080 });
await server.listen();
import { Client, Event, Packet } from "https://deno.land/x/[email protected]/mods.ts";
const client = new Client({ hostname: "127.0.0.2", port: 8080 });
await client.connect();
await client.write("Hello World!");
const server = new Server({ port: 8080 });
// Server listen
server.on(Event.listen, (server: Deno.Listener) => {
let addr = server.addr as Deno.NetAddr;
console.log(`Server listen ${addr.hostname}:${addr.port}`);
});
// Client connect
server.on(Event.connect, (client: Client) => {
console.log("New Client -", client.info());
});
// Receive packet
server.on(Event.receive, (client: Client, data: Packet, length: number) => {
console.log("Receive -", data.toString());
});
// Client close
server.on(Event.close, (client: Client) => {
console.log("Client close -", client.info());
});
// Server finish
server.on(Event.shutdown, () => {
console.log("Server is shutdown");
});
// Handle error
server.on(Event.error, (e) => {
console.error(e);
});
// Do
await server.listen(); // Start listen
server.broadcast("Hello")
server.broadcast("Hello", client) //Ignore broadcast
server.broadcast("Hello", [client]) //Ignore broadcast
import { Client, Packet, Event } from "https://deno.land/x/[email protected]/mods.ts";
const client = new Client({ hostname: "127.0.0.2", port: 8080 });
// Connection open
client.on(Event.connect, (client: Client) => {
console.log("Connect", client.conn?.remoteAddr);
});
// Receive message
client.on(Event.receive, (client: Client, data: Packet) => {
console.log("Receive", data.toString());
});
// Connection close
client.on(Event.close, (client: Client) => {
console.log("Close");
});
// Handle error
client.on(Event.error, (e) => {
console.error(e);
});
// Do
await client.connect(); // Start client connect
await client.write("Hello World"); // Send string data
await client.write(new Uint8Array()); // Send Uint8Array data
client.close();
const packet = new Packet();
const packetFromUint8Array = new Packet(new Uint8Array());
const packetFromString = new Packet("Hello");
const packetFromPacket = new Packet(packet);
packet.toString(); // Type of string
packet.toData(); // Type of Uint8Array
packet.length(); // Type of number
packet.append(packetFromString) // Add content to the end of the packet
packet.prepend(packetFromString) // Add content at the beginning of packet
- Echo server Echo server
- Chat server TCP Chat
- Client TCP Client
See MIT License. All rights reserved. GGalJJak 2021.