Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDDD97 Web Programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lawrence Zawahri
TDDD97 Web Programming
Commits
6ac1a6b4
Commit
6ac1a6b4
authored
3 years ago
by
Johan Thörnblom
Browse files
Options
Downloads
Patches
Plain Diff
almost done
parent
89a54cc7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Lab_3/Twidder/server.py
+43
-15
43 additions, 15 deletions
Lab_3/Twidder/server.py
Lab_3/Twidder/static/client.js
+53
-29
53 additions, 29 deletions
Lab_3/Twidder/static/client.js
with
96 additions
and
44 deletions
Lab_3/Twidder/server.py
+
43
−
15
View file @
6ac1a6b4
...
@@ -29,6 +29,7 @@ sock = Sock(app)
...
@@ -29,6 +29,7 @@ sock = Sock(app)
app
.
debug
=
True
app
.
debug
=
True
session
=
{
'
token
'
:
'
email
'
}
session
=
{
'
token
'
:
'
email
'
}
signedInUser
=
{
'
email
'
:
'
wsObj
'
}
@app.route
(
'
/
'
)
@app.route
(
'
/
'
)
def
root
():
def
root
():
...
@@ -65,31 +66,47 @@ def input_has_error(input):
...
@@ -65,31 +66,47 @@ def input_has_error(input):
@sock.route
(
'
/myServer/api
'
)
@sock.route
(
'
/myServer/api
'
)
def
echo
(
w
s
):
def
echo
(
s
ocket
):
while
True
:
while
True
:
if
not
ws
:
# Making sure we have a valid socket
if
not
socket
:
return
# Making sure message format is OK and store email & token in string
data
=
socket
.
receive
()
try
:
myEmail
=
json
.
loads
(
data
)[
"
email
"
]
except
:
signedInUser
[
email
].
close
()
return
try
:
myToken
=
json
.
loads
(
data
)[
"
token
"
]
except
:
signedInUser
[
email
].
close
()
return
return
data
=
ws
.
receive
()
email
=
json
.
loads
(
data
)[
"
email
"
]
token
=
json
.
loads
(
data
)[
"
token
"
]
# Nonse so far:
print
(
signedInUser
)
# if email in session.values():
# sign out if I am logged in somewhere else
# toDelete = ""
for
email
in
list
(
signedInUser
.
keys
()):
# for key, value in session.items():
if
email
==
myEmail
:
# if value == email:
print
(
"
You got kicked out
"
)
# toDelete = key
signedInUser
[
email
].
send
(
json
.
dumps
({
"
action
"
:
"
signOut
"
}))
# print(key, value)
signedInUser
[
email
].
close
()
signedInUser
.
pop
(
email
)
print
(
signedInUser
)
print
(
"
- - - -
"
+
token
)
# Put socket in global dict so server knows my connection is open
ws
.
send
(
data
)
signedInUser
[
myEmail
]
=
socket
socket
.
send
(
json
.
dumps
({
"
action
"
:
"
signIn
"
}))
@app.route
(
"
/myServer/sign_in
"
,
methods
=
[
'
POST
'
])
@app.route
(
"
/myServer/sign_in
"
,
methods
=
[
'
POST
'
])
def
sign_in
():
def
sign_in
():
"""
Sign in user
"""
"""
Sign in user
"""
print
(
"
shoit
"
)
# Validate Email
# Validate Email
tmp
=
input_has_error
(
'
email
'
)
tmp
=
input_has_error
(
'
email
'
)
if
tmp
[
0
]:
if
tmp
[
0
]:
...
@@ -179,8 +196,19 @@ def sign_out():
...
@@ -179,8 +196,19 @@ def sign_out():
if
tmp
[
0
]:
if
tmp
[
0
]:
return
jsonify
({}),
tmp
[
1
]
return
jsonify
({}),
tmp
[
1
]
print
(
signedInUser
)
# Close my socket
myEmail
=
session
[
token
]
try
:
signedInUser
[
myEmail
].
close
()
signedInUser
.
pop
(
myEmail
)
except
:
pass
# samma sak som ingenting
print
(
signedInUser
)
# set user to not logged in
# set user to not logged in
session
.
pop
(
token
)
session
.
pop
(
token
)
return
jsonify
({}),
204
# "Successfully signed out")
return
jsonify
({}),
204
# "Successfully signed out")
...
...
This diff is collapsed.
Click to expand it.
Lab_3/Twidder/static/client.js
+
53
−
29
View file @
6ac1a6b4
...
@@ -10,6 +10,49 @@ var socket;
...
@@ -10,6 +10,49 @@ var socket;
// source "/Users/lorenzo/OneDrive - Linköpings universitet/Skola/DI3B/TDDD97/virtualenv/bin/activate"
// source "/Users/lorenzo/OneDrive - Linköpings universitet/Skola/DI3B/TDDD97/virtualenv/bin/activate"
function
connectWithSocket
()
{
let
token
=
localStorage
.
getItem
(
"
currentUser
"
);
// Changes the view to profile view and loads user info
document
.
getElementById
(
"
error
"
).
innerHTML
=
"
You are signed in
"
;
displayView
();
setUserDetails
(
"
home
"
);
// Establish web socket
socket
=
new
WebSocket
(
'
ws://
'
+
document
.
domain
+
'
:5000/myServer/api
'
);
socket
.
onopen
=
function
(
event
)
{
let
myEmail
=
localStorage
.
getItem
(
"
homeEmail
"
);
// Todo - ändra så att token skickas i header istället
this
.
send
(
JSON
.
stringify
({
token
:
token
,
email
:
myEmail
}));
console
.
log
(
"
Nu har jag skickat
"
);
}
socket
.
onmessage
=
function
(
event
)
{
let
response
=
JSON
.
parse
(
event
.
data
);
console
.
log
(
"
Nu fick jag svar
"
);
switch
(
response
[
"
action
"
])
{
case
"
signOut
"
:
console
.
log
(
response
);
// Reset token in the localStorage
localStorage
.
setItem
(
"
currentUser
"
,
""
);
localStorage
.
setItem
(
"
homeEmail
"
,
""
);
localStorage
.
setItem
(
"
browseEmail
"
,
""
);
// Changes the view to welcome view
displayView
();
document
.
getElementById
(
"
error
"
).
innerHTML
=
"
Signed Out, you signed in elsewhere
"
;
break
;
case
"
signIn
"
:
console
.
log
(
response
);
console
.
log
(
"
ja är signed in
"
);
break
;
}
}
}
// - - - - - Init Request Objects - - - - - //
// - - - - - Init Request Objects - - - - - //
// Sign In Request Object
// Sign In Request Object
...
@@ -19,30 +62,9 @@ signInRequest.onreadystatechange = function() {
...
@@ -19,30 +62,9 @@ signInRequest.onreadystatechange = function() {
let
errorMessage
=
document
.
getElementById
(
"
error
"
);
let
errorMessage
=
document
.
getElementById
(
"
error
"
);
if
(
this
.
status
==
204
)
{
if
(
this
.
status
==
204
)
{
errorMessage
.
innerHTML
=
"
You are signed in
"
;
let
token
=
this
.
getResponseHeader
(
"
Authorization
"
)
let
token
=
this
.
getResponseHeader
(
"
Authorization
"
)
localStorage
.
setItem
(
"
currentUser
"
,
token
);
localStorage
.
setItem
(
"
currentUser
"
,
token
);
connectWithSocket
();
// Changes the view to profile view and loads user info
displayView
();
setUserDetails
(
"
home
"
);
// Establish web socket
socket
=
new
WebSocket
(
'
ws://
'
+
document
.
domain
+
'
:5000/myServer/api
'
);
socket
.
onopen
=
function
(
event
)
{
let
myEmail
=
localStorage
.
getItem
(
"
homeEmail
"
);
// Todo - ändra så att token skickas i header istället
this
.
send
(
JSON
.
stringify
({
token
:
token
,
email
:
myEmail
}));
console
.
log
(
"
Nu har jag skickat
"
);
}
socket
.
onmessage
=
function
(
event
)
{
console
.
log
(
"
Nu fick jag svar
"
);
console
.
log
(
event
.
data
);
};
console
.
log
(
"
ja är signed in
"
);
}
}
else
if
(
this
.
status
==
400
)
{
else
if
(
this
.
status
==
400
)
{
errorMessage
.
innerHTML
=
"
Error 400: Incorrect format
"
;
errorMessage
.
innerHTML
=
"
Error 400: Incorrect format
"
;
...
@@ -66,12 +88,11 @@ signOutRequest.onreadystatechange = function() {
...
@@ -66,12 +88,11 @@ signOutRequest.onreadystatechange = function() {
if
(
this
.
status
==
204
)
{
if
(
this
.
status
==
204
)
{
// Reset token in the localStorage
// Reset token in the localStorage
//
localStorage.setItem("currentUser", "");
localStorage
.
setItem
(
"
currentUser
"
,
""
);
//
localStorage.setItem("homeEmail", "");
localStorage
.
setItem
(
"
homeEmail
"
,
""
);
//
localStorage.setItem("browseEmail", "");
localStorage
.
setItem
(
"
browseEmail
"
,
""
);
// Changes the view to welcome view
// Changes the view to welcome view
displayView
();
displayView
();
}
}
else
if
(
this
.
status
==
401
)
{
else
if
(
this
.
status
==
401
)
{
...
@@ -336,12 +357,16 @@ window.onload = function () {
...
@@ -336,12 +357,16 @@ window.onload = function () {
// All key/value pairs stored in out localStorage
// All key/value pairs stored in out localStorage
// If first time? check this:
// If first time? check this:
if
(
localStorage
.
getItem
(
"
currentUser
"
)
==
null
)
localStorage
.
setItem
(
"
currentUser
"
,
""
);
if
(
localStorage
.
getItem
(
"
homeEmail
"
)
==
null
)
if
(
localStorage
.
getItem
(
"
homeEmail
"
)
==
null
)
localStorage
.
setItem
(
"
homeEmail
"
,
""
);
localStorage
.
setItem
(
"
homeEmail
"
,
""
);
if
(
localStorage
.
getItem
(
"
browseEmail
"
)
==
null
)
if
(
localStorage
.
getItem
(
"
browseEmail
"
)
==
null
)
localStorage
.
setItem
(
"
browseEmail
"
,
""
);
localStorage
.
setItem
(
"
browseEmail
"
,
""
);
if
(
localStorage
.
getItem
(
"
currentUser
"
)
==
null
)
localStorage
.
setItem
(
"
currentUser
"
,
""
);
// Todo - problem
if
(
localStorage
.
getItem
(
"
currentUser
"
)
!=
""
)
connectWithSocket
();
// Initialize div objects (global variables)
// Initialize div objects (global variables)
welcomeDiv
=
document
.
getElementById
(
"
welcomeview
"
);
welcomeDiv
=
document
.
getElementById
(
"
welcomeview
"
);
...
@@ -431,7 +456,6 @@ function sign_in() {
...
@@ -431,7 +456,6 @@ function sign_in() {
else
{
else
{
// Sending sign_in request to "server"
// Sending sign_in request to "server"
signInRequest
.
open
(
"
POST
"
,
url
+
"
sign_in
"
,
true
);
signInRequest
.
open
(
"
POST
"
,
url
+
"
sign_in
"
,
true
);
signInRequest
.
setRequestHeader
(
"
Content-Type
"
,
"
application/json;charset=UTF-8
"
);
signInRequest
.
setRequestHeader
(
"
Content-Type
"
,
"
application/json;charset=UTF-8
"
);
signInRequest
.
send
(
JSON
.
stringify
({
email
:
email
,
password
:
password
}));
signInRequest
.
send
(
JSON
.
stringify
({
email
:
email
,
password
:
password
}));
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment