Subversion Repositories HomeAutomation

Rev

Rev 1959 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1651 runge 1
 
1959 runge 2
var Socket_Connections = {};
1651 runge 3
 
1959 runge 4
var SOCKET_STATE_CONNECTED      = 0x00;
5
var SOCKET_STATE_DISCONNECTED   = 0x01;
6
var SOCKET_STATE_ACCEPTED       = 0x02;
7
 
1962 runge 8
function Socket_OnNewData(socket_id, server_id, data)
1651 runge 9
{
1962 runge 10
  var id = socket_id;
11
 
12
  if (!Socket_Connections[id])
13
  {
14
    id = server_id;
15
  }
16
 
17
  if (Socket_Connections[id])
18
  {
19
    Socket_Connections[id]["ondata"](id, data);
20
  }
21
 
22
  return true;
1651 runge 23
}
24
 
1962 runge 25
function Socket_OnNewState(socket_id, server_id, state)
1651 runge 26
{
1962 runge 27
  var id = socket_id;
28
 
29
  if (!Socket_Connections[id])
30
  {
31
    id = server_id;
32
  }
33
 
34
  if (Socket_Connections[id])
35
  {
36
    Socket_Connections[id]["onstate"](id, state);
37
 
38
    if (state == SOCKET_STATE_DISCONNECTED)
39
    {
40
      delete Socket_Connections[id];
41
    }
42
  }
43
 
44
  return true;
1651 runge 45
}
46
 
1959 runge 47
function Socket_StartServer(port, ondata_callback, onstate_callback)
48
{
49
  socket_id = SocketExport_StartServer(port);
50
 
51
  if (socket_id > 0)
52
  {
53
    Socket_Connections[socket_id] = { "ondata" : ondata_callback, "onstate" : onstate_callback };
54
  }
55
 
56
  return socket_id;
57
}
58
 
1651 runge 59
function Socket_Connect(address, port, ondata_callback, onstate_callback)
60
{
1962 runge 61
  socket_id = SocketExport_Connect(address, port);
62
 
63
  if (socket_id > 0)
64
  {
65
    Socket_Connections[socket_id] = { "ondata" : ondata_callback, "onstate" : onstate_callback };
66
  }
67
 
68
  return socket_id;
1651 runge 69
}
70
 
1959 runge 71
function Socket_StopServer(socket_id)
72
{
73
  SocketExport_StopServer(socket_id);
74
 
75
  delete Socket_Connections[socket_id];
76
}
77
 
1651 runge 78
function Socket_Disconnect(socket_id)
79
{
1962 runge 80
  SocketExport_Disconnect(socket_id);
81
 
82
  delete Socket_Connections[socket_id];
1651 runge 83
}
84
 
85
function Socket_Send(socket_id, data)
86
{
1962 runge 87
  SocketExport_Send(socket_id, data);
1651 runge 88
}