Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
shrapnel
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
shrapnel
Commits
216926d0
Commit
216926d0
authored
Jun 30, 2012
by
Sam Rushing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
not my files
parent
cc7065ab
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
148 deletions
+0
-148
coro/http/demo/websocket/chat.html
coro/http/demo/websocket/chat.html
+0
-28
coro/http/demo/websocket/frontend.js
coro/http/demo/websocket/frontend.js
+0
-120
No files found.
coro/http/demo/websocket/chat.html
deleted
100644 → 0
View file @
cc7065ab
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"utf-8"
>
<title>
WebSockets - Simple chat
</title>
<style>
*
{
font-family
:
tahoma
;
font-size
:
12px
;
padding
:
0px
;
margin
:
0px
;
}
p
{
line-height
:
18px
;
}
div
{
width
:
500px
;
margin-left
:
auto
;
margin-right
:
auto
;}
#content
{
padding
:
5px
;
background
:
#ddd
;
border-radius
:
5px
;
border
:
1px
solid
#CCC
;
margin-top
:
10px
;
}
#input
{
border-radius
:
2px
;
border
:
1px
solid
#ccc
;
margin-top
:
10px
;
padding
:
5px
;
width
:
400px
;
}
#status
{
width
:
88px
;
display
:
block
;
float
:
left
;
margin-top
:
15px
;
}
</style>
</head>
<body>
<div
id=
"content"
></div>
<div>
<span
id=
"status"
>
Connecting...
</span>
<input
type=
"text"
id=
"input"
disabled=
"disabled"
/>
</div>
<script
src=
"//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"
></script>
<script
src=
"frontend.js"
></script>
</body>
</html>
coro/http/demo/websocket/frontend.js
deleted
100644 → 0
View file @
cc7065ab
$
(
function
()
{
"
use strict
"
;
// for better performance - to avoid searching in DOM
var
content
=
$
(
'
#content
'
);
var
input
=
$
(
'
#input
'
);
var
status
=
$
(
'
#status
'
);
// my color assigned by the server
var
myColor
=
false
;
// my name sent to the server
var
myName
=
false
;
// if user is running mozilla then use it's built-in WebSocket
window
.
WebSocket
=
window
.
WebSocket
||
window
.
MozWebSocket
;
// if browser doesn't support WebSocket, just show some notification and exit
if
(
!
window
.
WebSocket
)
{
content
.
html
(
$
(
'
<p>
'
,
{
text
:
'
Sorry, but your browser doesn
\'
t
'
+
'
support WebSockets.
'
}
));
input
.
hide
();
$
(
'
span
'
).
hide
();
return
;
}
// open connection
var
connection
=
new
WebSocket
(
'
ws://127.0.0.1:9001/chat
'
);
connection
.
onopen
=
function
()
{
// first we want users to enter their names
input
.
removeAttr
(
'
disabled
'
);
status
.
text
(
'
Choose name:
'
);
};
connection
.
onerror
=
function
(
error
)
{
// just in there were some problems with conenction...
content
.
html
(
$
(
'
<p>
'
,
{
text
:
'
Sorry, but there
\'
s some problem with your
'
+
'
connection or the server is down.</p>
'
}
));
};
// most important part - incoming messages
connection
.
onmessage
=
function
(
message
)
{
// try to parse JSON message. Because we know that the server always returns
// JSON this should work without any problem but we should make sure that
// the massage is not chunked or otherwise damaged.
try
{
var
json
=
JSON
.
parse
(
message
.
data
);
}
catch
(
e
)
{
console
.
log
(
'
This doesn
\'
t look like a valid JSON:
'
,
message
.
data
);
return
;
}
// NOTE: if you're not sure about the JSON structure
// check the server source code above
if
(
json
.
type
===
'
color
'
)
{
// first response from the server with user's color
myColor
=
json
.
data
;
status
.
text
(
myName
+
'
:
'
).
css
(
'
color
'
,
myColor
);
input
.
removeAttr
(
'
disabled
'
).
focus
();
// from now user can start sending messages
}
else
if
(
json
.
type
===
'
history
'
)
{
// entire message history
// insert every single message to the chat window
for
(
var
i
=
0
;
i
<
json
.
data
.
length
;
i
++
)
{
addMessage
(
json
.
data
[
i
].
author
,
json
.
data
[
i
].
text
,
json
.
data
[
i
].
color
,
new
Date
(
json
.
data
[
i
].
time
));
}
}
else
if
(
json
.
type
===
'
message
'
)
{
// it's a single message
input
.
removeAttr
(
'
disabled
'
);
// let the user write another message
addMessage
(
json
.
data
.
author
,
json
.
data
.
text
,
json
.
data
.
color
,
new
Date
(
json
.
data
.
time
));
}
else
{
console
.
log
(
'
Hmm..., I
\'
ve never seen JSON like this:
'
,
json
);
}
};
/**
* Send mesage when user presses Enter key
*/
input
.
keydown
(
function
(
e
)
{
if
(
e
.
keyCode
===
13
)
{
var
msg
=
$
(
this
).
val
();
if
(
!
msg
)
{
return
;
}
// send the message as an ordinary text
connection
.
send
(
msg
);
$
(
this
).
val
(
''
);
// disable the input field to make the user wait until server
// sends back response
input
.
attr
(
'
disabled
'
,
'
disabled
'
);
// we know that the first message sent from a user their name
if
(
myName
===
false
)
{
myName
=
msg
;
}
}
});
/**
* This method is optional. If the server wasn't able to respond to the
* in 3 seconds then show some error message to notify the user that
* something is wrong.
*/
setInterval
(
function
()
{
if
(
connection
.
readyState
!==
1
)
{
status
.
text
(
'
Error
'
);
input
.
attr
(
'
disabled
'
,
'
disabled
'
).
val
(
'
Unable to comminucate
'
+
'
with the WebSocket server.
'
);
}
},
3000
);
/**
* Add message to the chat window
*/
function
addMessage
(
author
,
message
,
color
,
dt
)
{
content
.
append
(
'
<p><span style="color:
'
+
color
+
'
">
'
+
author
+
'
</span> @
'
+
+
(
dt
.
getHours
()
<
10
?
'
0
'
+
dt
.
getHours
()
:
dt
.
getHours
())
+
'
:
'
+
(
dt
.
getMinutes
()
<
10
?
'
0
'
+
dt
.
getMinutes
()
:
dt
.
getMinutes
())
+
'
:
'
+
message
+
'
</p>
'
);
}
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment