-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.R
129 lines (113 loc) · 4.07 KB
/
server.R
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
library(shiny)
library(stringr)
# Globally define a place where all users can share some reactive data.
vars <- reactiveValues(chat=NULL, users=NULL)
# Restore the chat log from the last session.
if (file.exists("chat.Rds")){
vars$chat <- readRDS("chat.Rds")
} else {
vars$chat <- "Welcome to Shiny Chat!"
}
#' Get the prefix for the line to be added to the chat window. Usually a newline
#' character unless it's the first line.
linePrefix <- function(){
if (is.null(isolate(vars$chat))){
return("")
}
return("<br />")
}
shinyServer(function(input, output, session) {
# Create a spot for reactive variables specific to this particular session
sessionVars <- reactiveValues(username = "")
# Track whether or not this session has been initialized. We'll use this to
# assign a username to unininitialized sessions.
init <- FALSE
# When a session is ended, remove the user and note that they left the room.
session$onSessionEnded(function() {
isolate({
vars$users <- vars$users[vars$users != sessionVars$username]
vars$chat <- c(vars$chat, paste0(linePrefix(),
tags$span(class="user-exit",
sessionVars$username,
"left the room.")))
})
})
# Observer to handle changes to the username
observe({
# We want a reactive dependency on this variable, so we'll just list it here.
input$user
if (!init){
# Seed initial username
sessionVars$username <- paste0("User", round(runif(1, 10000, 99999)))
isolate({
vars$chat <<- c(vars$chat, paste0(linePrefix(),
tags$span(class="user-enter",
sessionVars$username,
"entered the room.")))
})
init <<- TRUE
} else{
# A previous username was already given
isolate({
if (input$user == sessionVars$username || input$user == ""){
# No change. Just return.
return()
}
# Updating username
# First, remove the old one
vars$users <- vars$users[vars$users != sessionVars$username]
# Note the change in the chat log
vars$chat <<- c(vars$chat, paste0(linePrefix(),
tags$span(class="user-change",
paste0("\"", sessionVars$username, "\""),
" -> ",
paste0("\"", input$user, "\""))))
# Now update with the new one
sessionVars$username <- input$user
})
}
# Add this user to the global list of users
isolate(vars$users <- c(vars$users, sessionVars$username))
})
# Keep the username updated with whatever sanitized/assigned username we have
observe({
updateTextInput(session, "user",
value=sessionVars$username)
})
# Keep the list of connected users updated
output$userList <- renderUI({
tagList(tags$ul( lapply(vars$users, function(user){
return(tags$li(user))
})))
})
# Listen for input$send changes (i.e. when the button is clicked)
observe({
if(input$send < 1){
# The code must be initializing, b/c the button hasn't been clicked yet.
return()
}
isolate({
# Add the current entry to the chat log.
vars$chat <<- c(vars$chat,
paste0(linePrefix(),
tags$span(class="username",
tags$abbr(title=Sys.time(), sessionVars$username)
),
": ",
tagList(input$entry)))
})
# Clear out the text entry field.
updateTextInput(session, "entry", value="")
})
# Dynamically create the UI for the chat window.
output$chat <- renderUI({
if (length(vars$chat) > 500){
# Too long, use only the most recent 500 lines
vars$chat <- vars$chat[(length(vars$chat)-500):(length(vars$chat))]
}
# Save the chat object so we can restore it later if needed.
saveRDS(vars$chat, "chat.Rds")
# Pass the chat log through as HTML
HTML(vars$chat)
})
})