Skip to content

Instantly share code, notes, and snippets.

@champierre
Last active August 29, 2015 14:08
Show Gist options
  • Save champierre/2c3854bace3bd0c87d91 to your computer and use it in GitHub Desktop.
Save champierre/2c3854bace3bd0c87d91 to your computer and use it in GitHub Desktop.
send message to Scratch
# TCPSocket を使うため socket ライブラリを読み込みます
require "socket"
# localhost の TCP 番号 42001 番に接続します
socket = TCPSocket.open("localhost", 42001)
# 命令の内容
command = "broadcast \"a\""
# 命令の文字数(=バイト数)
length = command.length
# 10進数のバイト数をバイト(16進数)に変換
bytes = [length].pack("N")
# バイトに変換した命令のバイト数と、命令をつなげたものをスクラッチに送る
socket.write(bytes + command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment