Unverified Commit eb4b41e5 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix unmatched WebSocket upgrade requests being closed (#57245)

We should not close unmatched sockets as it might be handled by user's server.

Fixes #56996
Fixes #49334
Fixes #55299
parent 4a37c2d1
...@@ -602,8 +602,9 @@ export async function initialize(opts: { ...@@ -602,8 +602,9 @@ export async function initialize(opts: {
if (parsedUrl.protocol) { if (parsedUrl.protocol) {
return await proxyRequest(req, socket as any, parsedUrl, head) return await proxyRequest(req, socket as any, parsedUrl, head)
} }
// no match close socket
socket.end() // If there's no matched output, we don't handle the request as user's
// custom WS server may be listening on the same path.
} catch (err) { } catch (err) {
console.error('Error handling upgrade request', err) console.error('Error handling upgrade request', err)
socket.end() socket.end()
......
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
'use client'
import { useEffect, useState } from 'react'
import io from 'socket.io-client'
let socket
export default function Home() {
const [value, setValue] = useState('')
const socketInitializer = async () => {
// We call this just to make sure we turn on the websocket server
await fetch('/api/socket')
socket = io(undefined, {
path: '/api/my_awesome_socket',
})
socket.on('connect', () => {
console.log('Connected', socket.id)
})
socket.on('newIncomingMessage', (msg) => {
setValue(msg)
})
}
const sendMessageHandler = async (e) => {
if (!socket) return
const value = e.target.value
setValue(value)
socket.emit('createdMessage', value)
}
useEffect(() => {
socketInitializer()
}, [])
return <input value={value} onChange={sendMessageHandler} />
}
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'socket-io',
{
files: __dirname,
dependencies: {
'socket.io': '4.7.2',
'socket.io-client': '4.7.2',
'utf-8-validate': '6.0.3',
bufferutil: '4.0.8',
},
},
({ next }) => {
it('should support socket.io without falling back to polling', async () => {
let requestsCount = 0
const browser1 = await next.browser(next.url, '/')
const browser2 = await next.browser(next.url, '/', {
beforePageLoad(page) {
page.on('request', () => {
requestsCount++
})
},
})
const input1 = await browser1.elementByCss('input')
const input2 = await browser2.elementByCss('input')
await input1.fill('hello world')
await check(() => input2.inputValue(), /hello world/)
const currentRequestsCount = requestsCount
await input1.fill('123456')
await check(() => input2.inputValue(), /123456/)
// There should be no new requests (polling) and using the existing WS connection
expect(requestsCount).toBe(currentRequestsCount)
})
}
)
import { Server } from 'socket.io'
function onSocketConnection(io, socket) {
const createdMessage = (msg) => {
socket.broadcast.emit('newIncomingMessage', msg)
}
socket.on('createdMessage', createdMessage)
}
export default function handler(req, res) {
if (res.socket.server.io) {
res.end()
return
}
const io = new Server(res.socket.server, {
path: '/api/my_awesome_socket',
})
res.socket.server.io = io
const onConnection = (socket) => {
onSocketConnection(io, socket)
}
io.on('connection', onConnection)
res.end()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment