Skip to content

Commit 4dc6477

Browse files
committed
initial commit
0 parents  commit 4dc6477

File tree

13 files changed

+698
-0
lines changed

13 files changed

+698
-0
lines changed

.editorconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; https://editorconfig.org/
2+
root = true
3+
4+
[*]
5+
insert_final_newline = true
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
indent_size = 2
10+
11+
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
12+
indent_style = tab
13+
indent_size = 4
14+
15+
[*.md]
16+
indent_size = 4
17+
trim_trailing_whitespace = false
18+
19+
[*.yml,*.yaml]
20+
end_of_line = lf
21+
insert_final_newline = true
22+
indent_style = space
23+
indent_size = 2
24+
trim_trailing_whitespace = true
25+
26+
[Dockerfile]
27+
indent_size = 4

.github/workflows/build.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-go@v4
16+
with:
17+
go-version: '1.21'
18+
19+
- name: Build
20+
run: go build -v ./...
21+
22+
- name: Test
23+
run: go test -v ./...

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
# Go workspace file
18+
go.work

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Merix
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# NetGate
2+
![GitHub](https://img.shields.io/github/license/netvoid-labs/netgate)
3+
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/netvoid-labs/netgate)
4+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/netvoid-labs/netgate/Build)
5+
![GitHub repo size](https://img.shields.io/github/repo-size/netvoid-labs/netgate)
6+
7+
## Summary
8+
NetGate is Multiplayer Framework developed in Go and using Websockets.
9+
10+
## Installation
11+
```
12+
go get github.com/netvoid-labs/netgate
13+
```
14+
15+
## License
16+
```
17+
MIT
18+
```

client.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package netgate
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"sync"
7+
8+
"github.com/google/uuid"
9+
"github.com/gorilla/websocket"
10+
)
11+
12+
type Client struct {
13+
id string //unique id
14+
conn *websocket.Conn //websocket connection
15+
room *RoomInterface //room where client is
16+
lock *sync.Mutex //lock
17+
}
18+
19+
func newClient(conn *websocket.Conn) *Client {
20+
return &Client{
21+
id: strings.ReplaceAll(uuid.New().String(), "-", ""),
22+
conn: conn,
23+
room: nil,
24+
lock: &sync.Mutex{},
25+
}
26+
}
27+
28+
func (c *Client) GetID() string {
29+
return c.id
30+
}
31+
32+
func (c *Client) GetRoom() *RoomInterface {
33+
return c.room
34+
}
35+
36+
func (c *Client) Send(data []byte) error {
37+
c.lock.Lock()
38+
defer c.lock.Unlock()
39+
40+
if c.conn == nil {
41+
return errors.New("client is not connected")
42+
}
43+
44+
err := c.conn.WriteMessage(websocket.BinaryMessage, data)
45+
if err != nil {
46+
return err
47+
}
48+
49+
return nil
50+
}
51+
52+
func (c *Client) Disconnect() error {
53+
c.lock.Lock()
54+
defer c.lock.Unlock()
55+
56+
if c.conn == nil {
57+
return errors.New("client is not connected")
58+
}
59+
60+
err := c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
61+
if err != nil {
62+
return err
63+
}
64+
65+
return c.close()
66+
}
67+
68+
func (c *Client) close() error {
69+
err := c.conn.Close()
70+
if err != nil {
71+
return err
72+
}
73+
74+
c.conn = nil
75+
76+
return nil
77+
}
78+
79+
func (c *Client) read() ([]byte, error) {
80+
if c.conn == nil {
81+
return nil, errors.New("client is not connected")
82+
}
83+
84+
_, msg, err := c.conn.ReadMessage()
85+
if err != nil {
86+
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
87+
return nil, err
88+
}
89+
return nil, err
90+
}
91+
92+
return msg, nil
93+
}

cmd/client/main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
"github.com/gorilla/websocket"
12+
)
13+
14+
func main() {
15+
endpoint := flag.String("endpoint", "ws://localhost:4555", "server endpoint")
16+
roomId := flag.String("id", "12345678", "room id")
17+
18+
ws, _, err := websocket.DefaultDialer.Dial(*endpoint+"/"+*roomId, nil)
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
go func() {
24+
defer ws.Close()
25+
26+
for {
27+
mt, data, err := ws.ReadMessage()
28+
if err != nil {
29+
log.Println("Error while reading message:", err)
30+
return
31+
}
32+
log.Println("recv message type:", mt, "message:", string(data))
33+
}
34+
}()
35+
36+
//user write input in console and whenr press enter, send to the server
37+
go func() {
38+
log.Println("Write a message to send to the server:")
39+
for {
40+
var msg string
41+
_, err := fmt.Scanln(&msg)
42+
if err != nil {
43+
log.Println("Error while reading message:", err)
44+
return
45+
}
46+
ws.WriteMessage(websocket.BinaryMessage, []byte(msg))
47+
}
48+
}()
49+
50+
//wait for ctrl+c
51+
log.Println("Press Ctrl+C to stop")
52+
c := make(chan os.Signal, 1)
53+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
54+
<-c
55+
56+
log.Println("Closing connection...")
57+
58+
//send close
59+
err = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
60+
if err != nil {
61+
log.Println("Error while sending close message:", err)
62+
}
63+
64+
ws.Close()
65+
}

cmd/server/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/netvoid-labs/netgate"
7+
)
8+
9+
func main() {
10+
server := netgate.NewNetGate()
11+
12+
server.Run(&TestGameRoom{})
13+
}
14+
15+
// TestGameRoom is a simple game that implements the RoomInterface
16+
type TestGameRoom struct {
17+
players map[string]*netgate.Client
18+
}
19+
20+
func (s *TestGameRoom) Init() {
21+
s.players = make(map[string]*netgate.Client)
22+
}
23+
24+
func (s *TestGameRoom) Destroy() {
25+
s.players = nil
26+
}
27+
28+
func (s *TestGameRoom) Update(tick int64) {
29+
}
30+
31+
func (s *TestGameRoom) ClientJoin(client *netgate.Client) {
32+
s.players[client.GetID()] = client
33+
}
34+
35+
func (s *TestGameRoom) ClientLeave(client *netgate.Client) {
36+
delete(s.players, client.GetID())
37+
}
38+
39+
func (s *TestGameRoom) ClientData(client *netgate.Client, data []byte) {
40+
log.Printf("Client %s data: %s\n", client.GetID(), string(data))
41+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/netvoid-labs/netgate
2+
3+
go 1.21
4+
5+
require (
6+
github.com/google/uuid v1.3.0
7+
github.com/gorilla/websocket v1.5.0
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
2+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
4+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

0 commit comments

Comments
 (0)