Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
http服务器正在运行,但在与计时器一起使用时接收404_Http_Go_Socket.io - Fatal编程技术网

http服务器正在运行,但在与计时器一起使用时接收404

http服务器正在运行,但在与计时器一起使用时接收404,http,go,socket.io,Http,Go,Socket.io,我正在运行以下代码块,以便在本地主机上运行http服务器: server, err := socketio.NewServer(nil) . . . go server.Serve() defer server.Close() router.Handle("/socket.io/", server) log.Fatal(http.ListenAndServe(":"+port, router)) 在这段代码之前,我在main函数中调用initTime

我正在运行以下代码块,以便在本地主机上运行http服务器:

server, err := socketio.NewServer(nil)
.
.
.
go server.Serve()
defer server.Close()

router.Handle("/socket.io/", server)

log.Fatal(http.ListenAndServe(":"+port, router))
在这段代码之前,我在
main
函数中调用initTimer方法。以下是
initTimer
方法:

func initTimer() {

    stop := time.After(3 * time.Second)
    for {
        select {
        case <-stop:
            fmt.Println("EXIT: 3 seconds")
        case <-time.After(10 * time.Second):
            println("ali")

}
func initTimer(){
停止:=时间后(3*时间秒)
为了{
挑选{

case您发布的
for
循环将永远不会终止。一旦您的
开关
案例需要
中断
for循环。如果您不中断
for
循环,您对
http.listendandserve
的调用将永远不会运行。

如果您想独立于http服务器运行计时器功能,只需在自己的goroutine中启动即可:

go initTimer()
log.Fatal(http.ListenAndServe(":"+port, router))

您可以发布更多的HTTP代码吗?您必须了解,在
路由器中注册的HanderFunc在其自己的Goroutine中执行,因此它们之外的计时器等东西必须通过通道或其他方式进行通信。@jcdl没有更多的代码:(我只是设置为http路径,并在gorillamux路由器中使用它来运行http服务器。您是在尝试延迟每个请求还是仅启动http服务器?@jcdlİ我只是启动http服务器。计时器与http请求无关。上面列出的功能对另一个goroutine(即
http.server)没有任何影响。您需要发布更多的路由器/服务器设置。我无法在for循环外使用它。当我尝试此操作时,ide给出错误。此外,当我将break放入交换机机箱内时,它仍然无法从一开始工作:
ListenAndServe始终返回非零错误。
最干净的如果没有正在使用的端口,或者错误的TLS证书配置等在
http.Server.Shutdown()之后
ErrServerClosed
将返回错误
。哎哟!当然,ListendServe会阻止主goroutine!那么我该怎么运行呢timer@AliGürelli您需要在自己的goroutine中运行它。它可以工作,但现在case@Ali Gürelli尝试使用ticker