v icon indicating copy to clipboard operation
v copied to clipboard

websockets ipv6 doesn't seem to work

Open despiegk opened this issue 2 years ago • 3 comments

// mut ws := websocket.new_client('ws://localhost:30000')?
//THIS DOESNT WORK YET
mut ws := websocket.new_client('ws://[200:8b1a:83f8:91ad:b442:2ee9:2702:a428]:30000')?

result

2022-08-01 07:58:36 [INFO ] websocket server: start listen on port 30000
V panic: substr(40, 39) out of bounds (len=46)
v hash: b8438de
0   ping                                0x0000000104c02a1c string_substr + 304
1   ping                                0x0000000104c33480 net__urllib__parse_host + 1420
2   ping                                0x0000000104c32a94 net__urllib__parse_authority + 220
3   ping                                0x0000000104c32818 net__urllib__parse_url + 1828
4   ping                                0x0000000104c31f3c net__urllib__parse + 116
5   ping                                0x0000000104c6e6e8 net__websocket__parse_uri + 68
6   ping                                0x0000000104c6e48c net__websocket__new_client + 64
7   ping                                0x0000000104c731d8 main__start_client + 236
8   ping                                0x0000000104c73084 main__main + 176
9   ping                                0x0000000104c74b54 main + 84
10  dyld                                0x00000001050510f4 start + 520

I tried with hostname

ping6 kds
PING6(56=40+8+8 bytes) 200:8b1a:83f8:91ad:b442:2ee9:2702:a428 --> 200:8b1a:83f8:91ad:b442:2ee9:2702:a428
16 bytes from 200:8b1a:83f8:91ad:b442:2ee9:2702:a428, icmp_seq=0 hlim=64 time=6.340 m

sudo v run /Users/despiegk1/code/github/freeflowuniverse/rmb/websocket/ping.v
2022-08-01 12:00:58 [INFO ] websocket server: start listen on port 30000
2022-08-01 12:00:58 [INFO ] connecting to host ws://kds:30000
error on connect: dial_tcp failed for address kds:30000
panicing writing trying to write on a closed socket!
ws listen starts
2022-08-01 12:01:03 [INFO ] Starting client listener, server(false)...
2022-08-01 12:01:03 [INFO ] Quit client listener, server(false)...
ws listen ends
end of start client


no luck

testscript

module main

import time
import net.websocket

fn main() {
	go start_server()
	time.sleep(100 * time.millisecond)
	start_client()?
}

// start_server starts the websocket server, it receives messages
// and send it back to the client that sent it
fn start_server() ? {
	mut s := websocket.new_server(.ip6, 30000, '')
	// Make that in execution test time give time to execute at least one time
	s.ping_interval = 100
	s.on_connect(fn (mut s websocket.ServerClient) ?bool {
		// Here you can look att the client info and accept or not accept
		// just returning a true/false
		if s.resource_name != '/' {
			return false
		}
		// println(s.resource_name)
		return true
	})?
	s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
		println("server receives:${msg.payload.bytestr()}")
		// println(msg.opcode)
		ws.write(msg.payload, msg.opcode) or { panic(err) }
	})
	s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
		// println('client ($ws.id) closed connection')
	})
	s.listen() or { println('error on server listen: $err') }
	unsafe {
		s.free()
	}
}

// start_client starts the websocket client, it writes a message to
// the server and prints all the messages received
fn start_client() ? {
	// mut ws := websocket.new_client('ws://localhost:30000')?
	//THIS DOESNT WORK YET
	// mut ws := websocket.new_client('ws://[200:8b1a:83f8:91ad:b442:2ee9:2702:a428]:30000')?
	mut ws := websocket.new_client('ws://kds:30000')?
	

	// mut ws := websocket.new_client('wss://echo.websocket.org:443')?
	// use on_open_ref if you want to send any reference object
	// ws.on_open(fn (mut ws websocket.Client) ? {
	// 	println('open!')
	// })
	// use on_error_ref if you want to send any reference object
	ws.on_error(fn (mut ws websocket.Client, err string) ? {
		println('error: $err')
	})
	// // use on_close_ref if you want to send any reference object
	ws.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
		println('client closed: $reason')
	})
	// use on_message_ref if you want to send any reference object
	ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
		if msg.payload.len > 0 {
			message := msg.payload.bytestr()
			println('client got type: $msg.opcode payload:$message')
		}
	})
	// you can add any pointer reference to use in callback
	// t := TestRef{count: 10}
	// ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef) ? {
	// // println('type: $msg.opcode payload:\n$msg.payload ref: $r')
	// }, &r)
	ws.connect() or { println('error on connect: $err') }
	go write_echo(mut ws) // or { println('error on write_echo $err') }
	println("ws listen starts")
	ws.listen() or { println('error on listen $err') }
	println("ws listen ends")
	unsafe {
		ws.free()
	}
	println("end of start client")
}

fn write_echo(mut ws websocket.Client) ? {
	message := 'echo this'
	for i := 0; i <= 10; i++ {
		// Server will send pings every 30 seconds
		ws.write_string(message) or { println('panicing writing $err') }
		time.sleep(100 * time.millisecond)
	}
	ws.close(1000, 'normal') or { println('panicing $err') }
}

despiegk avatar Aug 01 '22 10:08 despiegk

The first part "Correct parsing of ip6 address" is not implemented (yet). . I did get it to work just using localhost or the hostname. I used the script above and just used my own hostname. I tried this on linux. I have both a ip4 and ip6 address on the computer. Tell me about your setup please @despiegk.

helto4real avatar Aug 02 '22 10:08 helto4real

@helto4real an m1 machine

medvednikov avatar Aug 04 '22 01:08 medvednikov

kds is name on hosts file (is not the hostname) my setup is a m1 apple computer

despiegk avatar Aug 04 '22 04:08 despiegk

Hi @despiegk , this should be fixed in master. Let me know if you have any more issues.

joe-conigliaro avatar Aug 12 '22 15:08 joe-conigliaro