Skip to content

Instantly share code, notes, and snippets.

@patrikbego
patrikbego / WebSocketClient.js
Last active June 6, 2023 10:41
Simple Javascript WebSocket Client
// This is a Websocket client (can be run in browser console)
// ref client https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
// The standalone server can be found here https://gist.github.com/patrikbego/447c40f8eaee709d7a6a2d2cadbfd046
const URL = 'ws://localhost:8000';
let websocket;
function updateStatus(status) {
if (status === 'connected') {
console.log('All good');
@patrikbego
patrikbego / WebSocketStandalone.java
Last active June 6, 2023 10:48
Standalone Java Websocket Server
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
@patrikbego
patrikbego / native-node-file-upload.js
Last active June 23, 2023 16:38
Native / pure / no-npm file upload REST API
/**
* This is native /pure /no-npm node.js file uploader.
*
* CLIENT FUNCTION: export function uploadFile(file) {
* fetch("http://localhost:8080/sys/upload", {
* method: 'POST',
* headers: { "Content-Type": "multipart/form-data;"},
* body: file }).then(response => response.json()
* ).then(
* success => console.log(success)