Sub Process_Globals
Private sock As Socket
Private astream As AsyncStreams
Private bc As ByteConverter
Private timer1 As Timer
Type Message(Text As String, Sound As Boolean, Badge As Int, ExpirationDate As Long, _
HighPriority As Boolean, _
userID As String, _
userName As String)
End Sub
Public Sub Start
bc.LittleEndian = False
timer1.Initialize("timer1", 3000) 'this timer is used when trying to reconnect
Connect
End Sub
Private Sub Connect
If sock.IsInitialized Then sock.Close
Dim sock As Socket
sock.InitializeSSL("sock", File.OpenInput(Main.config.Get("iPushKeystore"), ""), Main.config.Get("iPushKeystorePassword"))
sock.Connect(Main.config.Get("iGateway"), Main.config.Get("iGateWayPort"), 30000)
Log(">>> iOSPush: connect")
End Sub
Private Sub sock_Connected (Successful As Boolean)
If Successful Then
timer1.Enabled = False
Log(">>> iOSPush: Socket connected.")
If astream.IsInitialized Then astream.Close
astream.Initialize(sock.InputStream, sock.OutputStream, "astream")
Else
Log(">>> iOSPush: Error connecting socket: " & LastException)
Reconnect
End If
End Sub
Private Sub Reconnect
Log(">>> iOSPush: Trying to reconnect...")
timer1.Enabled = True
End Sub
Private Sub Timer1_Tick
timer1.Enabled = False
Connect
End Sub
Private Sub astream_NewData (Buffer() As Byte)
If Buffer.Length >=6 Then
Log(">>> iOSPush: status=" & Buffer(1))
Else
Log(">>> iOSPush: Invalid response")
End If
End Sub
Private Sub astream_Error
Log(">>> iOSPush: error: " & LastException)
Reconnect
End Sub
Private Sub astream_Terminated
Log(">>> iOSPush: terminated")
Reconnect
End Sub
Public Sub SendMessageTo(Devices As List, msg As Message)
Log(">>> iOSPush: Send message to userID: " & msg.userID & ", userName: " & msg.userName & ", Badge: " & msg.Badge)
Dim out As OutputStream
out.InitializeToBytesArray(0)
For Each device As String In Devices
Dim FrameData As OutputStream
FrameData.InitializeToBytesArray(0)
WriteItem(FrameData, 1, bc.HexToBytes(device))
Dim jg As JSONGenerator
Dim m As Map = CreateMap("alert": CreateMap("title": "___TITLE___", _
"body": msg.Text), "badge": msg.Badge , "userid" : msg.userID, "username": msg.userName)
If msg.Sound Then m.Put("sound", "default")
jg.Initialize(CreateMap("aps":m))
'jg.Initialize(CreateMap("aps":CreateMap("content-available": 1)))
WriteItem(FrameData,2, jg.ToString.GetBytes("UTF8"))
WriteItem(FrameData,3, bc.IntsToBytes(Array As Int(Rnd(0, 9999999))))
WriteItem(FrameData,4, bc.IntsToBytes(Array As Int(msg.ExpirationDate / 1000)))
Dim p As Byte
If msg.HighPriority Then p = 10 Else p = 5
WriteItem(FrameData,5, Array As Byte(p))
Dim payload() As Byte = FrameData.ToBytesArray
out.WriteBytes(Array As Byte(2), 0, 1)
out.WriteBytes(bc.IntsToBytes(Array As Int(payload.Length)), 0, 4)
out.WriteBytes(payload, 0, payload.Length)
Next
astream.Write(out.ToBytesArray)
End Sub
Private Sub WriteItem(out As OutputStream, id As Byte, data() As Byte)
out.WriteBytes(Array As Byte(id), 0, 1)
out.WriteBytes(bc.ShortsToBytes(Array As Short(data.Length)), 0, 2)
out.WriteBytes(data, 0, data.Length)
End Sub