Difference between revisions of "Bokeh-Server: Terima UDP dengan x axis datetime"

From OnnoWiki
Jump to navigation Jump to search
(New page: ==client.py== import sys import time import socket import random # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the p...)
 
Line 49: Line 49:
 
   
 
   
 
  # Create a TCP/IP socket
 
  # Create a TCP/IP socket
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
   
 
   
 
  # Bind the socket to the port
 
  # Bind the socket to the port
  server_address = ('192.168.0.100', 2000)
+
  server_address = ('192.168.0.100', 8888)
 
  print >>sys.stderr, 'starting up on %s port %s' % server_address
 
  print >>sys.stderr, 'starting up on %s port %s' % server_address
 
  sock.bind(server_address)
 
  sock.bind(server_address)
 
# Listen for incoming connections
 
sock.listen(1)
 
 
   
 
   
 
  show(p)
 
  show(p)
Line 65: Line 62:
 
   
 
   
 
  while True:
 
  while True:
    # Wait for a connection
 
    print >>sys.stderr, 'waiting for a connection'
 
    connection, client_address = sock.accept()
 
 
   
 
   
 
     try:
 
     try:
Line 73: Line 67:
 
   
 
   
 
         while True:
 
         while True:
             data = connection.recv(20)
+
             data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 
             print data
 
             print data
 
   
 
   

Revision as of 10:02, 3 January 2016

client.py

import sys
import time
import socket
import random

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('192.168.0.100', 2000)
sock.connect(server_address)

try:
    for i in xrange(100000):
        m0 = random.random()
        message = "%1.4f" % m0  
        print >>sys.stderr, 'sending ' , message
        sock.sendall(message)
        time.sleep(0.5) 

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

Server

bokeh-server --ip 192.168.0.100


server.py

import sys
import time
import datetime
import socket
from bokeh.plotting import figure, output_server, show, cursession

output_server("raw",url='http://192.168.0.100:5006')

# Visualization workflow
rx = [0]
x = [0]
n = datetime.datetime.now()
x = ['%d.%d'%(n.minute*60+n.second,n.microsecond)]
p = figure(x_axis_type="datetime")
p.line(x, rx, name='raw')

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('192.168.0.100', 8888)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

show(p)

renderer = p.select(dict(name="raw"))
ds = renderer[0].data_source

while True:

    try:
        print >>sys.stderr, 'connection from', client_address

        while True:
            data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
            print data

            if len(data) == 0:
               break

            rx += [data]
            n  = datetime.datetime.now()
            x  += ['%d.%d'%(n.minute*60+n.second,n.microsecond)]

            ds.data["x"] = x
            ds.data["rmag_x"] = rx
            cursession().store_objects(ds)

            time.sleep(0.01)

    finally:
        # Clean up the connection
        connection.close()