forked from livecodingyoutube/livecodingyoutube.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxserver.rb
executable file
·106 lines (81 loc) · 3.71 KB
/
maxserver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
# file: wapOSCserver.rb
#
#
# works with a Max patch wapOSCtester.maxpat
#
# receives Osc messages from Max and sends them out via WebSockets to a forked version of WebAudioPlayground (WAP)
#
# note this program will crash if you send Osc messages to it before the WebSockets connect is initialized from the
# WAP program running in the Web browser
#
require 'rubygems'
require 'osc-ruby'
# require 'patron'
require 'em-websocket'
require 'json'
msg_data = 0.0
socket = EM::WebSocket
# initialize OSC
#
@osc_server = OSC::Server.new( 8000 ) # this is where I'm listening
@osc_client = OSC::Client.new( 'localhost', 9000 ) # this is the target, ie a Max patch on this computer
# add methods to server which listens for OSC messages
# slider value
# can we wild card these? yes
# @osc_server.add_method '/mod/0/slider/0' do | message |
# @osc_server.add_method '/mod/*/*/*' do | message |
# @osc_server.add_method '/point' do | message |
# puts "#{message.ip_address}:#{message.ip_port} -- #{message.address} -- #{message.to_a}" # for debugging to console
# msg_data1 = message.to_a[0].to_f
# msg_data2 = message.to_a[1].to_f
# socket.send("#{message.address} #{msg_data1} #{msg_data2}") # send message out over WebSockets (should check if socket is open first)
# end
@osc_server.add_method '*' do | message |
puts "#{message.address} -- #{message.to_a}"
socket.send("#{message.address}/#{message.to_a}")
end
# @osc_server.add_method '/search' do | message |
# puts "#{message.ip_address}:#{message.ip_port} -- #{message.address} -- #{message.to_a}" # for debugging to console
# msg_data = message.to_a[0]
# socket.send("#{message.address}||#{msg_data}") # send message out over WebSockets (should check if socket is open first)
# end
# @osc_server.add_method '/select' do | message |
# puts "#{message.ip_address}:#{message.ip_port} -- #{message.address} -- #{message.to_a}" # for debugging to console
# msg_data = message.to_a[0]
# socket.send("#{message.address}||#{msg_data}") # send message out over WebSockets (should check if socket is open first)
# end
# @osc_server.add_method '/play' do | message |
# puts "#{message.ip_address}:#{message.ip_port} -- #{message.address} -- #{message.to_a}" # for debugging to console
# socket.send("#{message.address}") # send message out over WebSockets (should check if socket is open first)
# end
# @osc_server.add_method '/pause' do | message |
# puts "#{message.ip_address}:#{message.ip_port} -- #{message.address} -- #{message.to_a}" # for debugging to console
# socket.send("#{message.address}") # send message out over WebSockets (should check if socket is open first)
# end
# fire up the Osc server
Thread.new do
@osc_server.run
end
puts "waiting for Osc messages..."
# ip = '192.168.1.104'
ip = 'localhost'
# now start up websockets server
EM.run {
EM::WebSocket.run(:host => ip, :port => 1234) do |ws|
ws.onopen { |handshake| # this runs when browser (client) opens a connection
puts "WebSocket connection open"
socket = ws # assign websocket to global pointer so we can use it elsewhere in the program
# Access properties on the EM::WebSocket::Handshake object, e.g.
# path, query_string, origin, headers
# Publish message to the client
ws.send "Hello Client, you connected to #{handshake.path}"
}
ws.onclose { puts "Connection closed" } # this runs when connection closes
ws.onmessage { |msg| # this runs when a message comes back from the websockets client
puts "Recieved message: #{msg}"
ws.send "Pong: #{msg}" # a sort of loopback
}
end
}
# this program ends will you kill it with ctrl-c