Fork me on GitHub
#beginners
<
2016-09-20
>
eslachance00:09:46

Ok I'm... having an issue with websockets, I'm using guiznado, which is just about the only lib I've found with a proper example of a websocket client in clojure and not in clojurescript... ๐Ÿ˜’ Does anyone have experience with doing this with guiznado, or maybe an alternate ws server lib I could use with some examples?

eslachance00:09:09

I did... maybe it's my noobness, but I'm seeing the websocket client as being CLJS, not CLJ...

eslachance00:09:22

so unless I can just use it as-is in CLJ... I dunno

eslachance00:09:40

Even then I don't even see any connect methods

shaun-mahood00:09:43

Oh might be, I didn't think that far :)

eslachance00:09:38

That's my issue... most people don't feel the need to create a websocket client on the server-side...

verma02:09:57

luckyevie: Iโ€™ve used guiznado before, a while ago but it did work alright for me, wait I think I used it in an opensource project, let me see if I can get you a link ...

verma02:09:51

luckyevie: I am using it here to connect to the slack RTM websocket: https://github.com/verma/clj-slackbot/blob/master/src/clj_slackbot/comms/slack_rtm.clj#L4

eslachance02:09:03

Thanks Verma! You (I think?) already sent this to me over a week ago and admitedly it's the basis of pretty much all my knowledge of websockets in Clojure ^_^

eslachance02:09:25

(Would have gotten to this issue sooner but discord and node keep pulling me back with urgent or interesting issues)

verma02:09:55

donโ€™t think it was me luckyevie but goodluck :thumbsup: ๐Ÿ™‚

verma02:09:35

๐Ÿ™‚

eslachance02:09:00

I do have a lot more complexity to my program though - I have around 2 pages of flowchart data for opcodes coming in an out of the Discord API. It's just a question of using the proper case though. I do have a friend helping me... or rather, who will after his WoW dailies (ahhhh, games. The eternal method of pushing back deadlines)

verma02:09:38

the only think I can probably suggest is that abstract the websocket out early into something clojure-esqe (e.g. core.async) and just make it sufficiently error resilient.

eslachance02:09:01

I'm actually using core.async, I fell in love with it immediately.

eslachance02:09:05

So I at least got that!

verma02:09:46

:thumbsup:

eslachance05:09:45

Uhm... Let's say I define a function like the following, and I want to have a way to... stop the go-loop on command. How would I go about doing this?

(defn ping-pong [out-pipe delay]
  (let [counter (atom 0)
        next-id #(swap! counter inc)]
    (go-loop []
      (<! (timeout delay))
      (println "Sending a ping request")
      (>! out-pipe {:op 1 :d (next-id)} )
      (recur))))

eslachance05:09:45

(actually I'd also need to reset the Atom to 0)

eslachance05:09:55

(btw I did figure out my websockets issue, I wasn't defining any arguments in the :on-error function but the error message was somewhat vague about it)

plexus13:09:42

There's a trick for that using an extra channel to signal "stop", and then "select" on multiple channels

plexus13:09:04

On the subway now but I can give you an example when I get home

plexus13:09:40

Might be better ways but that's how I've seen it done before

dominicm13:09:47

๐Ÿ‘ to the control-chan technique

dominicm13:09:16

(if (not kill-chan)
      (let [kill-chan (async/chan)]
        (async/go-loop []
          (let [[event ch] (async/alts! [input-chan kill-chan])]
            (if (= ch kill-chan)
              (async/close! kill-chan)
              (do
                (process-event! event events)
                (recur)))))
        (assoc this :kill-chan kill-chan))
      this)
I use this in a component, you'll need to tweak it slightly for your use case. To kill the loop:
(async/>!! kill-chan :kill)

plexus13:09:07

yup, that's the one. alts! will read from whichever channel has something to give it, and it returns the thing that came of the channel+the channel itself

plexus13:09:44

then you can do things based on which channel it came from

eslachance14:09:56

Awesome! Thank you