Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server not receiving messages from client #60

Open
darienmiller88 opened this issue Jun 9, 2021 · 1 comment
Open

Server not receiving messages from client #60

darienmiller88 opened this issue Jun 9, 2021 · 1 comment

Comments

@darienmiller88
Copy link

My server is receiving connections from the front end just fine, but emitting messages doesn't seem work. Is there something wrong with my syntax?

Front end:

const API_URL = "http://localhost:3311/socket.io/"
let socket = io.connect(API_URL, {transports: ['websocket']})

socket.on("chat-message", (data) => {
    console.log(data)
    appendMessage("other-message", data)
})

inputForm.addEventListener("submit", e => {
    e.preventDefault()

    const message = inputBox.value
    socket.emit("client-message", message, (result) => {
        console.log(result)
    })

    inputForm.reset()
})

Back end:

socket.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
		fmt.Println("id:", c.Id(), "ip:", c.Ip())
		c.Join("room")
})

socket.On("client-message", func(c *gosocketio.Channel, message string) string  {
                fmt.Println("message from id:", c.Id(), ":", message)
                
                c.BroadcastTo("room", "chat-message", message)
                return "message sent!"
})

router.Handle("/socket.io/", socket)
@davidh16
Copy link

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants