Skip to content

Commit

Permalink
feat(server): add websocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 10, 2024
1 parent aeb177c commit 72c7437
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "github.com/dezh-tech/immortal/server"

func main() {
s := server.NewServer()
err := s.Start()
if err != nil {
panic(err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/mailru/easyjson v0.7.7
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
golang.org/x/net v0.29.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
63 changes: 63 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package server

import (
"errors"
"io"
"log"
"net/http"

// TODO::: replace with https://github.com/coder/websocket.
"github.com/dezh-tech/immortal/types/envelope"
"golang.org/x/net/websocket"
)

type Server struct {
conns map[*websocket.Conn]bool
}

func NewServer() *Server {
return &Server{
conns: make(map[*websocket.Conn]bool),
}
}

func (s *Server) Start() error {
http.Handle("/ws", websocket.Handler(s.handleWS))
err := http.ListenAndServe(":3000", nil) //nolint

return err
}

func (s *Server) handleWS(ws *websocket.Conn) {
// TODO::: replace with logger.
log.Printf("new connection: %s\n", ws.RemoteAddr())

// TODO::: make it concurrent safe.
s.conns[ws] = true

s.readLoop(ws)
}

func (s *Server) readLoop(ws *websocket.Conn) {
buf := make([]byte, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

// TODO::: replace with logger.
log.Printf("error in connection handling: %s\n", err)

// TODO::: drop connection?
continue
}

msg := buf[:n]
env := envelope.ParseMessage(msg)

// TODO::: replace with logger.
log.Printf("received envelope: %s\n", env.String())
}
}
9 changes: 9 additions & 0 deletions types/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ func (e *Event) IsValid() bool {
// TODO::: replace with libsecp256k1 (C++ version).
return sig.Verify(hash[:], pubkey)
}

func (e *Event) String() string {
ee, err := e.Encode()
if err != nil {
return ""
}

return string(ee)
}
2 changes: 1 addition & 1 deletion types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ContainsKind(target Kind, arr []Kind) bool {
return false
}

// Escaping strings for JSON encoding according to RFC8259.
// EscapeString for JSON encoding according to RFC8259.
// Also encloses result in quotation marks "".
func EscapeString(dst []byte, s string) []byte {
dst = append(dst, '"')
Expand Down

0 comments on commit 72c7437

Please sign in to comment.