go-socket.io icon indicating copy to clipboard operation
go-socket.io copied to clipboard

after build, show up runtime error: invalid memory address or nil pointer dereference [BUG]

Open bennya8 opened this issue 5 years ago • 1 comments

Describe the bug running the demo code binary apps on centos and osx platforms

in the browser using socket.io js make a connection, a runtime error shows up.

when using 'go run' command to execute the demo code, all the functions perfectly fine. BUT after building the binary apps, with above two platforms both show up the same errors.

To Reproduce Build with README.md demo code, and connect with the browser using socket.io library

Server-side

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/googollee/go-socket.io"
)

func main() {
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	server.OnConnect("/", func(s socketio.Conn) error {
		s.SetContext("")
		fmt.Println("connected:", s.ID())
		return nil
	})
	server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
		fmt.Println("notice:", msg)
		s.Emit("reply", "have "+msg)
	})
	server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
		s.SetContext(msg)
		return "recv " + msg
	})
	server.OnEvent("/", "bye", func(s socketio.Conn) string {
		last := s.Context().(string)
		s.Emit("bye", last)
		s.Close()
		return last
	})
	server.OnError("/", func(e error) {
		fmt.Println("meet error:", e)
	})
	server.OnDisconnect("/", func(s socketio.Conn, msg string) {
		fmt.Println("closed", msg)
	})
	go server.Serve()
	defer server.Close()

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Serving at localhost:6000...")
	log.Fatal(http.ListenAndServe(":6000", nil))
}

Client-Side

demo.vue

<style lang="scss" scoped>
  #dmp-demo {
    button {
      background: #f2f2f2;
    }
  }
</style>
<script>
  import Vue from "vue";
  import VueSocketIO from "vue-socket.io";

  // init
  Vue.use(
    new VueSocketIO({
      debug: true,
      connection: 'http://localhost:6000',
      options: {
        path: "/socket.io"
      }
    })
  );

  export default {
    data() {
      return {
        isConnected: false
      };
    },
    sockets: {
      connect: function () {
        console.log("socket connected");
        this.isConnected = true;
      },
      disconnect() {
        console.log("socket disconnect");
        this.isConnected = false;
      }
    },
    methods: {
    }
  };
</script>
<template>
  <div id="dmp-demo">
    <div v-if="isConnected">
      server is online
    </div>
    <div v-else>
      server is offline
    </div>
  </div>
</template>

package.json

{
  "dependencies": {
    "vue": "^2.5.2",
    "vue-socket.io": "^3.0.7",
    "vuex": "^2.5.0",
  }
}

Expected behavior

2019/08/01 12:09:35 Serving at localhost:6000...
2019/08/01 12:09:40 http: panic serving 127.0.0.1:60629: runtime error: invalid memory address or nil pointer dereference
goroutine 33 [running]:
net/http.(*conn).serve.func1(0x11d12000)
	/usr/local/Cellar/go/1.12.7/libexec/src/net/http/server.go:1769 +0xe7
panic(0x31ace0, 0x643f58)
	/usr/local/Cellar/go/1.12.7/libexec/src/runtime/panic.go:522 +0x162
runtime/internal/atomic.Cas64(0x11cd047c, 0x0, 0x0, 0x1, 0x0)
	/usr/local/Cellar/go/1.12.7/libexec/src/runtime/internal/atomic/asm_386.s:60 +0xc
github.com/googollee/go-engine.io/payload.(*Payload).FlushOut(0x11cd0410, 0x3e1450, 0x11c925a0, 0x0, 0x0)
	/Users/Benny/go/pkg/mod/github.com/googollee/[email protected]/payload/payload.go:129 +0x106
github.com/googollee/go-engine.io/transport/polling.(*serverConn).ServeHTTP(0x11ca64d0, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/Users/Benny/go/pkg/mod/github.com/googollee/[email protected]/transport/polling/server.go:80 +0x361
github.com/googollee/go-engine%2eio.(*session).serveHTTP(0x11ca6540, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/Users/Benny/go/pkg/mod/github.com/googollee/[email protected]/session.go:197 +0x99
github.com/googollee/go-engine%2eio.(*Server).ServeHTTP(0x11c82230, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/Users/Benny/go/pkg/mod/github.com/googollee/[email protected]/server.go:191 +0x367
github.com/googollee/go-socket%2eio.(*Server).ServeHTTP(0x11c7c610, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/Users/Benny/go/pkg/mod/github.com/googollee/[email protected]/server.go:33 +0x36
net/http.(*ServeMux).ServeHTTP(0x64bf40, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/usr/local/Cellar/go/1.12.7/libexec/src/net/http/server.go:2375 +0x183
net/http.serverHandler.ServeHTTP(0x11d0a000, 0x3e4a90, 0x11c925a0, 0x11cb2200)
	/usr/local/Cellar/go/1.12.7/libexec/src/net/http/server.go:2774 +0x7e
net/http.(*conn).serve(0x11d12000, 0x3e5080, 0x11c8cc80)
	/usr/local/Cellar/go/1.12.7/libexec/src/net/http/server.go:1878 +0x78d
created by net/http.(*Server).Serve
	/usr/local/Cellar/go/1.12.7/libexec/src/net/http/server.go:2884 +0x266

Environment (please complete the following information):

  • Go version: 1.12.7
  • Operation System: OSX Mojave 10.14.5 and Linux CentOS 6.8/7.4

bennya8 avatar Aug 01 '19 04:08 bennya8

My local computer working just fine (macbook pro 2015 13 inch Mac os Majave 10.14.6) but when i send the code to server same error throwing..

frkn-aydn avatar Oct 08 '19 19:10 frkn-aydn