-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterClient.fsx
173 lines (155 loc) · 6.48 KB
/
TwitterClient.fsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#load "references.fsx"
#time "on"
open System
open Akka.Actor
open Akka.FSharp
open FSharp.Json
open WebSocketSharp
open Akka.Configuration
// number of user
let configuration =
ConfigurationFactory.ParseString(
@"akka {
log-config-on-start : on
stdout-loglevel : OFF
loglevel : OFF
actor {
provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
debug : {
receive : on
autoreceive : on
lifecycle : on
event-stream : on
unhandled : on
}
}
remote {
helios.tcp {
port = 8555
hostname = localhost
}
}
}")
let system = ActorSystem.Create("TwitterSim", configuration)
let echoServer = new WebSocket("ws://localhost:8080/websocket")
echoServer.OnOpen.Add(fun args -> System.Console.WriteLine("Open"))
echoServer.OnClose.Add(fun args -> System.Console.WriteLine("Close"))
echoServer.OnMessage.Add(fun args -> System.Console.WriteLine("Msg: {0}", args.Data))
echoServer.OnError.Add(fun args -> System.Console.WriteLine("Error: {0}", args.Message))
echoServer.Connect()
type MessageType = {
OperationName : string
UserName : string
Password : string
SubscribeUserName : string
TweetData : string
Queryhashtag : string
QueryAt : string
}
let TwitterClient (mailbox: Actor<string>)=
let mutable userName = ""
let mutable password = ""
let rec loop () = actor {
let! message = mailbox.Receive ()
let sender = mailbox.Sender()
let result = message.Split ','
let operation = result.[0]
if operation = "Register" then
userName <- result.[1]
password <- result.[2]
let serverJson: MessageType = {OperationName = "reg"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
return! loop()
else if operation = "Subscribe" then
let serverJson: MessageType = {OperationName = "subscribe"; UserName = userName; Password = password; SubscribeUserName = result.[1]; TweetData = ""; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
else if operation = "SendTweet" then
let serverJson: MessageType = {OperationName = "send"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = "tweet from "+userName+" "+result.[1]; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
sender <? "success" |> ignore
else if operation = "RecievedTweet" then
printfn "[%s] : %s" userName result.[1]
else if operation = "Querying" then
let serverJson: MessageType = {OperationName = "querying"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
sender <? "success" |> ignore
else if operation = "Logout" then
let serverJson: MessageType = {OperationName = "logout"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
else if operation = "QueryHashtags" then
let serverJson: MessageType = {OperationName = "#"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag = result.[1]; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
sender <? "success" |> ignore
else if operation = "QueryMentions" then
let serverJson: MessageType = {OperationName = "@"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag =""; QueryAt = result.[1]}
let json = Json.serialize serverJson
echoServer.Send json
sender <? "success" |> ignore
else if operation = "Retweet" then
let serverJson: MessageType = {OperationName = "retweet"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = result.[1]; Queryhashtag =""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
sender <? "success" |> ignore
else if operation = "Login" then
userName <- result.[1]
password <- result.[2]
let serverJson: MessageType = {OperationName = "login"; UserName = userName; Password = password; SubscribeUserName = ""; TweetData = ""; Queryhashtag = ""; QueryAt = ""}
let json = Json.serialize serverJson
echoServer.Send json
return! loop()
}
loop ()
let client = spawn system ("User"+(string 1)) TwitterClient
let rec readInput () =
Console.Write("Enter command: ")
let input = Console.ReadLine()
let inpMessage = input.Split ','
let serverOp = inpMessage.[0]
match (serverOp) with
| "Register" ->
let username = inpMessage.[1]
let password = inpMessage.[2]
client <! "Register,"+username+","+password
readInput()
| "Login" ->
let username = inpMessage.[1]
let password = inpMessage.[2]
client <! "Login,"+username+","+password
readInput()
| "Subscribe" ->
let username = inpMessage.[1]
client <! "Subscribe,"+username
readInput()
| "Send" ->
let message = inpMessage.[1]
client <! "SendTweet,"+message
readInput()
| "Query" ->
client <! "Querying"
readInput()
| "QueryHashtag" ->
client <! "QueryHashtags,"+inpMessage.[1]
readInput()
| "QueryMention" ->
client <! "QueryMentions,"+inpMessage.[1]
readInput()
| "Retweet" ->
client <! "Retweet,"+inpMessage.[1]
readInput()
| "Logout" ->
client <! "Logout"
readInput()
| "Exit" ->
printfn "Exiting Client!"
| _ ->
printfn "Invalid Input, Please refer the Report"
readInput()
readInput()
system.Terminate() |> ignore
0