On Thursday, May 19, 2016 at 5:35:17 PM UTC+2, Shakthi Kannan wrote:
> Hi,
>
> I have the following code snippet:
>
> === JavaScript ===
>
> var client = new net.Socket();
>
> client.connect(1337, '127.0.0.1', function() {
> console.log('Connected');
> client.write('Hello, server! Love, Client.');
> });
>
> client.on('data', function(data) {
> console.log('Received: ' + data);
> client.destroy(); // kill client after server's response
> });
>
> client.on('close', function() {
> console.log('Connection closed');
> });
>
> === END ===
>
> #1 How can I convert and use the above functionality in ClojureScript?
>
> #2 Is there a recommended approach to re-using JavaScript libraries
> with cljs? For example: https://github.com/diversario/node-ssdp
>
> Appreciate your help in this regard,
>
> Thanks!
>
> SK
>
> P.S.: Newbie to ClojureScript.
>
> --
> Shakthi Kannan
> http://www.shakthimaan.com
Hello,
# 1
straightforward solution (beware this is not a idiomatic ClojureScript
solution!, I haven't tried.):
(defn snippet []
(let [client (js/net.Socket.) ; creates a new instance of Socket
handle-connect #(do ; handles a connection
(js/console.log "Connected")
(.write client "Hello, server! Love,
Client."))
handle-on-data #(do ; handles data
(js/console.log (str "Received: " %))
(.destroy client))
handle-on-close #(js/console.log "Connection closed")]
(do ; supposed to call do macro, because of side effects
(.connect client 1337, '127.0.0.1') ; connect to the client
(.on client "data" handle-on-data) ; register a data handler
(.on client "close" handle-on-close))) ; register a close handler)
# 2
It's better to use a ClojureScript library if there are any. Because there are
written in ClojureScript way and it's easier to user their API. If there is no
library for your problem, so you can use a JS library via JS interop.
Lukas
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/clojurescript.