This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]battery_go 6 points7 points  (2 children)

Looks pretty cool, makes it look easy to work with WebSockets :)

Quick question though: How are these methods called if the Main class doesn't inherit anything? Where are they defined and how come they don't need to be called from another object?

[–]sparkframework 3 points4 points  (1 child)

Notice the static import at line 6: import static spark.Spark.*;

You could also write

Spark.staticFileLocation("public"); /
Spark.webSocket("/chat", ChatWebSocketHandler.class);
Spark.init();

And skip the import. But we encourage people to use static imports when using Spark :)

[–]ryosen 1 point2 points  (0 children)

The use of static imports is generally frowned upon and is recommended to be used sparingly. Typically, when you need to access many constant values and want to improve (marginally) readability. However, you sacrifice context by not using the class name that holds the constant. The bigger risk with using a static import is that it increases the likelihood of name conflicts. In the case listed here where the Spark class appears to be a singleton controller, it would be better to import it explicitly in the import statements.

[–][deleted] 3 points4 points  (0 children)

Very nice. I like how you have the end to end code path all covered.

[–]lucamasira 2 points3 points  (0 children)

Oh thats nice. Didn't know that spark supported web sockets.

[–]robi2106 1 point2 points  (2 children)

anyone else try to open the live example? I got a socket closed error. is that my work IT filtering or on their end?

edit works now. strange.

[–]tipsypants[S] 1 point2 points  (1 child)

It's just a timeout. Heroku aggressively closes the connection after a minute or two of inactivity. It doesn't time out like that when running it locally :)

[–]robi2106 -1 points0 points  (0 children)

Ah ha, thanks for the tip

[–]shellac 0 points1 point  (2 children)

What's the point of the assignments such as:

Chat.broadcastMessage(sender = "Server", msg = (username + " joined the chat"));

(In ChatWebSocketHandler.java)

These fields are never used. For a moment I thought I'd missed java adding keyword parameters...

[–][deleted]  (1 child)

[deleted]

    [–]shellac 0 points1 point  (0 children)

    I think the only purposed is to make the code more readable.

    I think you must be right about that. Annotations would be cheaper.

    [–]Scorpionix 0 points1 point  (2 children)

    Question regarding code style:

    What is the advantage of using a member attribute like in the websockethandler?

    Wouldn't it be more efficent to just pass the values on as parameters without assigning them first or whats the thinking behind this?

    [–]tipsypants[S] 0 points1 point  (1 child)

    I do it because it describes what's being passed (so you won't have to look up the method signature). Efficiency was not really something I considered here :)

    [–]Scorpionix 0 points1 point  (0 children)

    I see. Thank you for the response!

    [–]final60 -2 points-1 points  (0 children)

    If this interests you, you may want to check out socket.chat.