ID:1928990
 
(See the best response by Doohl.)
Is anyone aware of any existing system for working with WebSockets in Byond?

I would love to get Byond to connect to a socket.io NodeJS server!
Best response
BYOND can accept input / return output to any external device communicating with the server on a socket via world/Topic. This isn't a native feature, however, so you kinda need to do a bit of tinkering and hacking of your own.

I've made this easier for you and posted some code that should help you in this endeavor:

http://www.byond.com/forum/?post=1249769#comment4843323
Wow, that looks very interesting. Thank you for the input.
I've noticed that the codebase I am working on uses the world Topic already, in order to listen for "players" to return the number of players.

As a short example:

/world/Topic(T, addr, master, key)
        if(T == "players")
                var/n = 0
                for(var/mob/M in player_list)
                        if(M.client)
                                n++
                return n


I craft a packet to send from Java:

\x00\x8314\x00\x00\x00\x00\x00?players\x00


Does that seem correct to you? I am connecting to the server fine with my socket, getting no errors. However, I am getting no kind of response.


return_packet = "\x00\x83[length(return_message) + 1][is_ascii ? "\x06" : "\x2a"][return_message]"


Are you accounting for the return packet as well?
Just a note, you don't actually need that 'players' topic thing. The default behavior of world/Topic() will let you send a 'ping' query which will return a similar number (it's actually the number of clients +1, so it'll return 1 when the world is live but empty).
I haven't, however I had thought that it would print something at least. Even if it were just garbled characters.

I should test that.
It seems like my code is hanging on reading input. Indicating to me that Byond isn't sending anything.

This is what I am using right now in my Java:

http://pastebin.com/0cH0ibw9
I see also on the server console that it picks up the Topic in your PHP code, but with my Java code it doesn't. The issue is in the sent request.

The only thing I can think of, looking at your PHP code, is the pack('n', ...) function.
Yeah, you need to find the Java equivalent of the pack() function. You can't just insert a raw integer into the byte stream like that, otherwise I'm pretty sure it'll just be converted to ASCII, which is not what you want.

You want to convert the integer to an unsigned 16-bit big-endian number before pasting it into the byte stream.