Skip to content

Commit 11122cd

Browse files
committed
library net examples
1 parent d3b88c8 commit 11122cd

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

library/net/HTTPClient.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'picrate'
2+
# HTTP Client.
3+
#
4+
# Starts a network client that connects to a server on port 80,
5+
# sends an HTTP 1.0 GET request, and prints the results.
6+
# Note that this code is not necessary for simple HTTP GET request:
7+
# Simply calling load_strings("http://www.processing.org") would do
8+
# the same thing as (and more efficiently than) this example.
9+
# This example is for people who might want to do something more
10+
# complicated later.
11+
class HTTPClient < Processing::App
12+
load_library 'net'
13+
include_package 'processing.net'
14+
15+
attr_reader :client, :data
16+
17+
def setup
18+
sketch_title 'Http Client'
19+
background(50)
20+
fill(200)
21+
# Connect to server on port 80
22+
@client = Client.new(self, 'www.ucla.edu', 80)
23+
# Use the HTTP "GET" command to ask for a Web page
24+
client.write("GET / HTTP/1.0\r\n")
25+
client.write("\r\n")
26+
end
27+
28+
def draw
29+
return unless client.available > 0
30+
data = client.read_string # ...then grab it and print it
31+
puts data
32+
end
33+
34+
def settings
35+
size(200, 200)
36+
end
37+
end
38+
39+
HTTPClient.new

library/net/shared_canvas_client.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'picrate'
2+
# Shared Drawing Canvas (Client)
3+
# by Alexander R. Galloway. Translated to JRubyArt by Martin Prout
4+
#
5+
# The Processing Client class is instantiated by specifying a remote address
6+
# and port number to which the socket connection should be made. Once the
7+
# connection is made, the client may read (or write) data to the server.
8+
# NB: Start the Shared Drawing Canvas (Server) program first
9+
class CanvasClient < Processing::App
10+
load_library :net
11+
include_package 'processing.net'
12+
13+
attr_reader :c
14+
15+
def settings
16+
size(450, 255)
17+
end
18+
19+
def setup
20+
sketch_title 'Client'
21+
background(204)
22+
stroke(0)
23+
frame_rate(5) # Slow it down a little
24+
# Replace with your server's IP and port
25+
@c = Client.new(self, '127.0.0.1', 12_345)
26+
end
27+
28+
def draw
29+
if mouse_pressed?
30+
# Draw our line
31+
stroke(255)
32+
line(pmouse_x, pmouse_y, mouse_x, mouse_y)
33+
# Send mouse coords to other person
34+
c.write(format("%d %d %d %d\n", pmouse_x, pmouse_y, mouse_x, mouse_y))
35+
end
36+
# Receive data from server
37+
return unless c.available > 0
38+
input = c.read_string
39+
# Split values into an array and convert to int
40+
data = input.split(' ').map(&:to_i)
41+
# Draw line using received coords
42+
stroke(0)
43+
line(*data)
44+
end
45+
end
46+
47+
CanvasClient.new

library/net/shared_canvas_server.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'picrate'
2+
# Shared Drawing Canvas (Server)
3+
# by Alexander R. Galloway. Translated to JRubyArt by Martin Prout
4+
#
5+
# A server that shares a drawing canvas between two computers.
6+
# In order to open a socket connection, a server must select a
7+
# port on which to listen for incoming clients and through which
8+
# to communicate. Once the socket is established, a client may
9+
# connect to the server and send or receive commands and data.
10+
# Get this program running and then start the Shared Drawing
11+
# Canvas (Client) program so see how they interact.
12+
class CanvasServer < Processing::App
13+
load_library :net
14+
include_package 'processing.net'
15+
16+
attr_reader :s
17+
18+
def settings
19+
size(450, 255)
20+
end
21+
22+
def setup
23+
sketch_title 'Server'
24+
background(204)
25+
stroke(0)
26+
frame_rate(5) # Slow it down a little
27+
@s = Server.new(self, 12_345) # Start a simple server on a port
28+
end
29+
30+
def draw
31+
if mouse_pressed?
32+
# Draw our line
33+
stroke(255)
34+
line(pmouse_x, pmouse_y, mouse_x, mouse_y)
35+
# Send mouse coords to other person
36+
s.write(format("%d %d %d %d\n", pmouse_x, pmouse_y, mouse_x, mouse_y))
37+
end
38+
# Receive data from client
39+
c = s.available
40+
return if c.nil?
41+
input = c.read_string
42+
# Split values into an array and convert to int
43+
data = input.split(' ').map(&:to_i)
44+
# Draw line using received coords
45+
line(*data)
46+
end
47+
end
48+
49+
CanvasServer.new

0 commit comments

Comments
 (0)