Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GBS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CS375
GBS
Merge requests
!9
Resolve "Define Object for Cell"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Resolve "Define Object for Cell"
5-rooms
into
main
Overview
0
Commits
4
Pipelines
0
Changes
4
Merged
jjd358
requested to merge
5-rooms
into
main
1 year ago
Overview
0
Commits
4
Pipelines
0
Changes
4
Closes
#5 (closed)
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
a8928b64
4 commits,
1 year ago
4 files
+
423
−
230
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
public/game.html
0 → 100644
+
141
−
0
View file @ a8928b64
Edit in single-file editor
Open in Web IDE
<!doctype html>
<html
lang=
"en"
>
<head>
<title>
WebSocket Example
</title>
</head>
<body>
<h1>
WebSocket Client
</h1>
<input
type=
"text"
id=
"messageInput"
placeholder=
"Type a message..."
/>
<button
id=
"sendButton"
>
Send
</button>
<div
id=
"messages"
></div>
<div
id=
"game"
>
<canvas
id=
"gameCanvas"
width=
"800"
height=
"600"
></canvas>
</div>
<script>
let
pathParts
=
window
.
location
.
pathname
.
split
(
"
/
"
);
let
roomId
=
pathParts
[
pathParts
.
length
-
1
];
const
joinMessage
=
{
type
:
"
join
"
,
data
:
`
${
roomId
}
`
};
let
moveMessage
=
{
type
:
"
move
"
,
data
:
"
up
"
,
pid
:
0
};
const
socket
=
new
WebSocket
(
"
ws://127.0.0.1:3001
"
,
`
${
roomId
}
`
);
const
canvas
=
document
.
getElementById
(
"
gameCanvas
"
);
const
width
=
canvas
.
width
;
const
height
=
canvas
.
height
;
let
gameBoardHandler
=
(
event
)
=>
{
let
ctx
=
canvas
.
getContext
(
"
2d
"
);
ctx
.
clearRect
(
0
,
0
,
width
,
height
);
ctx
.
fillStyle
=
"
black
"
;
ctx
.
fillRect
(
0
,
0
,
width
,
height
);
ctx
.
fillStyle
=
"
white
"
;
let
board
=
event
.
data
;
console
.
log
(
board
);
widthStep
=
width
/
board
[
0
].
length
;
heightStep
=
height
/
board
.
length
;
for
(
let
i
=
0
;
i
<
board
.
length
;
i
++
)
{
for
(
let
j
=
0
;
j
<
board
[
i
].
length
;
j
++
)
{
if
(
board
[
i
][
j
].
type
===
0
)
{
ctx
.
fillRect
(
j
*
widthStep
+
1
,
i
*
heightStep
+
1
,
widthStep
-
2
,
heightStep
-
2
,
);
}
if
(
board
[
i
][
j
].
type
===
1
)
{
ctx
.
fillStyle
=
"
red
"
;
ctx
.
fillRect
(
j
*
widthStep
+
1
,
i
*
heightStep
+
1
,
widthStep
-
2
,
heightStep
-
2
,
);
ctx
.
fillStyle
=
"
white
"
;
}
if
(
board
[
i
][
j
].
type
===
2
)
{
ctx
.
fillStyle
=
"
red
"
;
ctx
.
fillRect
(
j
*
widthStep
+
1
,
i
*
heightStep
+
1
,
widthStep
-
2
,
heightStep
-
2
,
);
ctx
.
fillStyle
=
"
white
"
;
}
if
(
board
[
i
][
j
].
type
===
3
)
{
ctx
.
fillStyle
=
"
blue
"
;
ctx
.
fillRect
(
j
*
widthStep
+
1
,
i
*
heightStep
+
1
,
widthStep
-
2
,
heightStep
-
2
,
);
ctx
.
fillStyle
=
"
white
"
;
}
}
}
};
let
newUserHandler
=
(
event
)
=>
{
moveMessage
.
pid
=
event
.
data
;
};
socket
.
addEventListener
(
"
open
"
,
()
=>
{
console
.
log
(
"
Connected to server
"
);
socket
.
send
(
JSON
.
stringify
(
joinMessage
));
});
socket
.
addEventListener
(
"
message
"
,
(
event
)
=>
{
try
{
let
msg
=
JSON
.
parse
(
event
.
data
);
if
(
msg
.
type
===
"
gameBoard
"
)
{
gameBoardHandler
(
msg
);
}
else
if
(
msg
.
type
===
"
newUser
"
)
{
newUserHandler
(
msg
);
}
else
{
const
messages
=
document
.
getElementById
(
"
messages
"
);
const
messageElement
=
document
.
createElement
(
"
div
"
);
messageElement
.
innerText
=
event
.
data
;
messages
.
appendChild
(
messageElement
);
}
}
catch
{
console
.
log
(
event
.
data
);
}
});
document
.
addEventListener
(
"
keydown
"
,
(
event
)
=>
{
if
(
event
.
key
===
"
ArrowUp
"
)
{
moveMessage
.
data
=
"
up
"
;
socket
.
send
(
JSON
.
stringify
(
moveMessage
));
}
if
(
event
.
key
===
"
ArrowDown
"
)
{
moveMessage
.
data
=
"
down
"
;
socket
.
send
(
JSON
.
stringify
(
moveMessage
));
}
if
(
event
.
key
===
"
ArrowLeft
"
)
{
moveMessage
.
data
=
"
left
"
;
socket
.
send
(
JSON
.
stringify
(
moveMessage
));
}
if
(
event
.
key
===
"
ArrowRight
"
)
{
moveMessage
.
data
=
"
right
"
;
socket
.
send
(
JSON
.
stringify
(
moveMessage
));
}
});
document
.
getElementById
(
"
sendButton
"
).
addEventListener
(
"
click
"
,
()
=>
{
const
messageInput
=
document
.
getElementById
(
"
messageInput
"
);
const
message
=
messageInput
.
value
;
socket
.
send
(
message
);
messageInput
.
value
=
""
;
});
</script>
</body>
</html>
Loading