【发布时间】:2021-09-04 11:17:49
【问题描述】:
我使用 socket.io-client 和 express 制作了一个用作客户端的 Web 服务器(因为我必须在其他项目中使用此表单)。
它发出发布的字符串,当从 io 服务器接收到 'boom' 发出时,它通过发送服务的字符串来响应。
第一次发布“heat_bomb”效果很好,但是当我第二次尝试时,“[ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头”发生在 socket.on() 中的 res.send(data) .
有没有办法在post请求产生的时候刷新,让每个请求都使用独立的响应?
app.ts
import express from 'express'
import {io} from 'socket.io-client'
import bodyParser from 'body-parser'
const app=express()
const PORT=8080
const socket=io(`http://localhost:2002`, {
query:{
hello:"merhaba"
}
})
app.use(bodyParser.urlencoded({extended:false}))
app.get('/', (req, res)=>{
res.sendFile(__dirname+`/index.html`)
})
app.post('/heat_bomb', (req, res)=>{
socket.emit('heat_bomb', req.body.elem)
socket.on('boom', (data)=>{
res.send(data)
})
})
app.listen(PORT, ()=>{
console.log(`Server Running: ${PORT}`)
})
index.html
$('#heat_button').click(function(){
console.log('heating bomb')
$.post('/heat_bomb', {elem: $('#input_number').val()},(data, status)=>{
console.log(data)
console.log('heated')
})
})
【问题讨论】:
标签: javascript express socket.io