Skip to content

Instantly share code, notes, and snippets.

@psidex
Last active October 13, 2023 16:18
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save psidex/cef1bf2290096816e0642d0e9da95a2f to your computer and use it in GitHub Desktop.
Weird issue
// Execute this normally in a browsers debug console.
const ws = new WebSocket('ws://127.0.0.1:8080/');
ws.onopen = async () => {
// Send different 1mb arrays.
for (let i = 0; i < 10; i++) {
const data = new Uint8Array(1024 * 1024);
for (let j = 0; j < 1024 * 1024; j++) {
data[j] = i;
}
ws.send(data);
}
};
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"net/http"
"time"
)
func websocketHandler(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
ws, _ := upgrader.Upgrade(w, r, nil)
defer ws.Close()
for i := 0; i < 10; i++ {
_, fileBytes, _ := ws.ReadMessage()
fmt.Println(fileBytes[0:10])
// Represents taking 1 second to process uploaded data.
time.Sleep(time.Second * 1)
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", websocketHandler).Methods("GET")
_ = http.ListenAndServe("127.0.0.1:8080", r)
}
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handleMessage(self):
print(self.data[0:10])
def handleConnected(self):
print(self.address, 'connected')
def handleClose(self):
print(self.address, 'closed')
server = SimpleWebSocketServer('127.0.0.1', 8080, SimpleEcho)
server.serveforever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment