A minor wierdness with websockets is that connect returns a string socket, 
whereas the event handlers have integer sockets.

I was hoping that 6!:2 allowed for event handlers to process messages, but it 
doesn't appear to.  Does such a command exist?  A bit like the old vb windows 
3.1 doevents()/yield()

Anyways, if you load the included code listing, and run 

testws a:

you will note that it fails to unload/close client sockets (line wd 'ws query 
1' returns open clients)

if you run the lines inside testws individually in repl, it works. (closes 
everything)



cocurrent 'z'
wssockets =: i.0 NB.
wsconnections =: i.0

deleteitem_z_ =:  {. , >:@[ }. ]
defaults1 =: ([`]@.(0=#@>@[))
defaults =: defaults1"0 0 f.
fixlenx =: 1 : (':';'((#y) {. x) u y')
iorinf =: iorinf =: (#@[ _:^:= i.)
inl_z_ =: (cocurrent@] ".@] [)"1 0
pD_z_ =:  1!:2&2

wscln_handler_z_=: 3 : 0
'evt sk'=. y
if. evt = jws_onMessage do.
smoutput 'client: ' wss0_jrx_
elseif. evt = jws_onOpen do.
smoutput 'connected ', ": sk
'onOpen 1' inl wsconnections {~ wssockets i. sk
elseif. evt = jws_onClose do.
smoutput 'disconnected ' , ": sk
'onClose 1' inl wsconnections {~ wssockets i. sk
elseif. evt = jws_onError do.
smoutput wsc0_jrx_
end.
EMPTY
)

wssvr_handler_z_=: 3 : 0
'evt sk'=. y
if. evt = jws_onMessage do.
smoutput 'server: ' wss0_jrx_
NB.  assert. _1 ~: r
elseif. evt = jws_onOpen do.
smoutput 'Serv connected ', ": sk
NB. pD datatype sk
'onOpen 1' inl ((sk);'server') conew 'wsconnection'
NB. 'onOpen 1' inl wsconnections {~ wssockets i. sk
elseif. evt = jws_onClose do.
smoutput 'Serv disconnected ' , ": sk
'onClose 1' inl wsconnections {~ wssockets i. sk
elseif. evt = jws_onError do.
smoutput 'error ', wss0_jrx_
end.
EMPTY
)

addsockconn =: 4 : 0
pD 'adds'
pD wssockets_z_ =: wssockets_z_ , x
pD wsconnections_z_ =: wsconnections_z_ , y
)
delsock=: 3 : 0
if. _ > i =. y iorinf~ wssockets do.
wssockets =: i deleteitem wssockets
wsconnections =: i deleteitem wsconnections
end.
)
delconn=: 3 : 0
if. _ > i =. y iorinf~ wsconnections_z_ do.
wssockets_z_ =: i deleteitem wssockets_z_
wsconnections_z_ =: i deleteitem wsconnections_z_
end.

)
coclass 'wsconnection'
connected =: 0
create =: 3 : 0
'socket conntype commands auth' =: y defaults fixlenx 
a:;'client';'testcommands';'authserver'
pD 18!:5 ''
socket (addsockconn ]]) 18!:5 ''
)
createServer =: 3 : 0

)
onOpen =: 3 : 'connected =: 1'
onClose =: 3 : 0
connected =: 0
delconn 18!:5 ''
)
close =: 3 : 'wd ''ws close '', ": socket'

NB. ( [conntype = 'client'];[commandslocale];[authlocale]) new_wsconnection_ 
'localhost 3000' ('addr port') > wsconnectionObj
new =: 4 : 0 NB. called for client connection
s =. 0 ". wd 'ws connect ws://', y
(s ; x) conew >18!:5 ''
)

NB. =========================================================
wd 'ws listen 3000'
cocurrent 'base'
testws =: 3 : 0
a: new_wsconnection_ 'localhost 3000'
a: new_wsconnection_ 'localhost 3000'
6!:3 (2)
'close 1' inl  '''client'' -: conntype' (] #~ inl) wsconnections_z_ NB. closes 
all clients (cascades cleanup)
6!:3 (1)
pD  'q0';wd 'ws query 0'
pD  'q1';wd 'ws query 1'
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to