Hi Andy,
a global map is the way to go, however writing an reading the map from
multiple go routines will fail (in your case different handle calls for
connections) because the map data structure is not safe for concurrent
use and must be coordinated. the way it is usually solved is by adding
another sync.Mutex (mutually exclusive) variable to ensure only one go
routine at a has access to the map.
https://godoc.org/sync#Mutex
just a heads up, have fun!
On 9/12/20 2:46 PM, Andy Hall wrote:
OK so I just moved the declaration of the map to the package itself
which makes it universal...all working as expected. Thanks.
On Saturday, September 12, 2020 at 1:35:49 PM UTC+1 Andy Hall wrote:
So this works fine...
func handleConnection(c net.Conn) {
// get user details
username := createUser(c, "Please enter you username (new users
will be created / existing users will be loaded): ")
// map username to connection
var m = make(map[string]net.Conn)
m[username] = c
n := len(m)
fmt.Println(strconv.Itoa(n))
But due to the concurrent nature of the function ( I can't have
player 1 blocking player 2 ) the map only ever has details of your
own connection...as seen from the output of len on the server...
$ go run game-server.go 6666
1
1
I guess I need to handle this outside of a goroutine...
On Saturday, September 12, 2020 at 7:55:34 AM UTC+1 Andy Hall wrote:
I think this is exactly what I need to do...thanks very much.
I'm looking forward to implementing it.
On Saturday, September 12, 2020 at 5:43:09 AM UTC+1 Shulhan wrote:
Pada tanggal Sab, 12 Sep 2020 02.54, Andy Hall
<andyjo...@gmail.com> menulis:
if I have multiple clients connected to a tcp server
and I wish to write back to specific connections I can
record the net.Conn value and then use the Write
method on it...but when using Println I get the
following for two clients...
&{{0xc000094000}}
&{{0xc000094080}}
which when testing with a simple write doesn't work...
package main
import "net"
var c net.Conn = "&{{0xc000094000}}"
func writeConn(c net.Conn) {
c.Write([]byte(string("Hello\n")))
}
func main() {
writeConn(c)
}
...and results in the following...
cannot use "&{{0xc000094000}}" (type string) as type
net.Conn in assignment
clearly using Println to output the net.Conn is not a
viable var to use so how could I do this ? I intend to
record each users net.Conn in a database which can
then be queried as required.
any help would be most greatly appreciated.
Either I miss something or Go has different socket
concept, but last time I learn this is not how the network
socket works in general.
First, &{{0xc000094000}} is the address of a variable. You
can't convert an address from string back to variable,
because that would be security issue. Usually socket
connection is signed integer, in C you can assign integer
value to variable let other process write into it. But in
Go, connection is an interface/structure.
If you want to record each users, you have two options:
1) Let the user send unique ID (for example their user ID
or email or username) on first accept.
2) Get unique ID from connection IP address (beware that
two or more connection may come from the same IP address).
You then must have a map that store unique ID as key and
net.Conn as value. So, if you want to send some value to
specific user, you query the map first and if exist then
you can proceeds.
I hope that helps.
--
You received this message because you are subscribed to the Google
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-nuts+unsubscr...@googlegroups.com
<mailto:golang-nuts+unsubscr...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/eff5d382-7bc5-4f2e-9752-a3c850318c0fn%40googlegroups.com
<https://groups.google.com/d/msgid/golang-nuts/eff5d382-7bc5-4f2e-9752-a3c850318c0fn%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/6084b595-2947-c23a-502c-14d431db715f%40mb0.org.