Open2

[Node.js/Golang] TCP/RSTでHTTP connを終了させる方法

oosawyoosawy

Node.js

const http = require('http')

const server = http.createServer((req, res) => {
  res.socket.resetAndDestroy()
})

server.listen(3000)
oosawyoosawy

Golang

package main

import (
	"net"
	"net/http"
)

func main() {
	http.ListenAndServe(":3000", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		conn, _, err := w.(http.Hijacker).Hijack()
		if err != nil {
			panic(err)
		}

		tcpConn := conn.(*net.TCPConn)
		tcpConn.SetLinger(0)
		tcpConn.Close()
	}))
}