Looks great @emc2! Looking forward to that project share!
Nope, not WebSockets—I mean good old standard TCP sockets, which are what the firmware TCPClient creates and what HTTP runs on top of. HTTP is just a TCP socket, with specific agreed-upon text formatting going back and forth.
On heroku, I don’t think you can run a generic TCP server, but I could be wrong.
Here are 3 minimal example TCP socket servers that can handle multiple concurrent connections. The node and ruby examples are just based on the language docs. For python, I basically used one from a Stack Overflow post.
JavaScript
var net = require('net');
var server = new net.Server();
server.on('connection', function (socket) {
socket.on('data', function (buffer) {
console.log('received data:', buffer.toString());
socket.write(buffer);
});
});
server.listen(3000);
Ruby
require 'socket'
server = TCPServer.new 3000
loop do
Thread.start(server.accept) do |client|
loop do
data = client.gets
puts "received data: #{data}"
client.puts data
end
end
end
Python
import socket, threading
class ClientThread(threading.Thread):
def __init__(self, ip, port, socket):
threading.Thread.__init__(self)
self.ip, self.port, self.socket = ip, port, socket
def run(self):
data = "_"
while len(data):
data = clientsock.recv(1024)
print "received data: " + data
clientsock.sendall(data)
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind(("0.0.0.0", 3000))
threads = []
while True:
tcpsock.listen(4)
(clientsock, (ip, port)) = tcpsock.accept()
newthread = ClientThread(ip, port, clientsock)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()