-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7345ad
commit a53e376
Showing
5 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
package goeventbus | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type Server interface { | ||
Listen() | ||
} | ||
|
||
type tcpServer struct { | ||
address, path string | ||
} | ||
|
||
func (s tcpServer) Listen() { | ||
|
||
listener, err := net.Listen("tcp", s.address) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
defer listener.Close() | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
continue | ||
} | ||
|
||
handleClient(conn) | ||
} | ||
} | ||
|
||
func NewServer() Server { | ||
return tcpServer{} | ||
func NewServer(address, path string) Server { | ||
return tcpServer{address: address, path: path} | ||
} | ||
|
||
func handleClient(conn net.Conn) { | ||
defer conn.Close() | ||
|
||
buffer := make([]byte, 1024) | ||
|
||
for { | ||
// Read data from the client | ||
n, err := conn.Read(buffer) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
// Process and use the data (here, we'll just print it) | ||
fmt.Printf("Received: %s\n", buffer[:n]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package goeventbus | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
go NewServer("localhost:8080", "/").Listen() | ||
|
||
conn, err := net.Dial("tcp", "localhost:8080") | ||
assert.Nil(t, err) | ||
|
||
defer conn.Close() | ||
|
||
data := []byte("Hello, Server!") | ||
_, err = conn.Write(data) | ||
assert.Nil(t, err) | ||
} |