Skip to content

Snippets

Wang Renxin edited this page Mar 22, 2018 · 1 revision

Network

Server

Setup:

CONNS = "<tcp://10088"

def svr_recv(msg, addr)
	t = "Recv: " + msg + " from " + addr
	msgbox(t)
	n.broadcast(msg, true) ' Broadcasts to all other connected clients except the receiving one.
enddef

n = net()
n.set("data_type", "string")
n.open
(
	CONNS,
	call(svr_recv), ' Data received callback.
	lambda (addr) ( print "+<: ", addr; ), ' Connection established callback.
	lambda (addr) ( print "-<: ", addr; ) ' Connection disconnected callback.
)

Broadcast:

input "Send: ", m$
print m$;
n.broadcast(m$)

Client

Setup:

CONNC = ">tcp://127.0.0.1:10088" ' Modify me with valid address.

def clt_recv(msg, addr)
	t = "Recv: " + msg + " from " + addr
	msgbox(t)
enddef

n = net()
n.set("data_type", "string")
n.open
(
	CONNC,
	call(clt_recv), ' Data received callback.
	lambda (addr) ( print "+>: ", addr; ), ' Connection established callback.
	lambda (addr) ' Connection closed callback.
	(
		print "->: ", addr;
		n.close() ' Closes self.
		n = nil
	)
)

Send:

input "Send: ", m$
print m$;
n.send(m$)