This repository was archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 850
This repository was archived by the owner on Sep 29, 2024. It is now read-only.
Getting Bad Request #381
Copy link
Copy link
Closed
Labels
Description
When emitting events from client, I got Bad Request
POST http://localhost:8500/socket.io/?EIO=3&transport=polling&t=1578039627529-2&sid=4 400 (Bad Request)
And on the server console, it said 'resume'
Here's my client code:
var socket = io();
var mesageForm = document.getElementById('send-container');
var mesageContainer = document.getElementById('message-container');
var mesageInput = document.getElementById('message-input');
const name = prompt('What is your name?');
appendMessage('You' + ' joined');
socket.emit('new-user', name);
socket.on('chat-message', data => {
appendMessage(data)
});
socket.on('user-connected', name => {
appendMessage(name + ' joined')
});
mesageForm.addEventListener('submit', function (e) {
e.preventDefault();
const message = mesageInput.value;
socket.emit('send-chat-message', message);
mesageInput.value = ''
});
function appendMessage(msg) {
const messageElement = document.createElement('div');
messageElement.innerText = msg;
mesageContainer.append(messageElement)
}And here's my server code
package main
import (
socketio "github.com/googollee/go-socket.io"
"github.com/labstack/echo"
)
type Sock struct {
sock socketio.Conn
Name string
}
var socks = make(map[string]Sock)
func main() {
io, _ := socketio.NewServer(nil)
app := echo.New()
app.Static("/", ".")
app.Any("/socket.io/", func(s echo.Context) error {
io.ServeHTTP(s.Response().Writer, s.Request())
return nil
})
io.OnConnect("/", func(s socketio.Conn) error {
socks[s.ID()] = Sock{
sock: s,
}
s.Emit("chat-message", "Server: Hello World")
return nil
})
io.OnEvent("/", "new-user", func(s socketio.Conn, name string) {
sk := socks[s.ID()]
sk.Name = name
for k, v := range socks {
if k != s.ID() {
v.sock.Emit("user-connected", name)
}
}
})
io.OnEvent("/", "send-chat-message", func(s socketio.Conn, msg string) {
for k, v := range socks {
if k != s.ID() {
v.sock.Emit("chat-message", msg)
}
}
})
go io.Serve()
defer io.Close()
app.Start(":8500")
}It's annoying. What is causing this error?