forked from darkcris1/svelte-chat-app-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cris Fandino
committed
Feb 4, 2021
1 parent
c4d3b8a
commit 5e2de70
Showing
13 changed files
with
190 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
node_modules/ | ||
|
||
client/node_modules/ | ||
client/node_modules/` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,26 @@ | ||
<script> | ||
import { onMount } from 'svelte' | ||
import io from 'socket.io-client' | ||
import Tailwindcss from './Tailwindcss.svelte' | ||
import Login from './components/Login.svelte' | ||
import ChatRoom from './components/ChatRoom.svelte' | ||
import { ID_KEY } from './const' | ||
import { joinRoom, loggedUser, users } from './store' | ||
let username, room, data | ||
const socket = io('http://localhost:3000', { | ||
transports: ['websocket', 'polling'], | ||
}) | ||
$: console.log($users) | ||
let usernameValue = '', | ||
roomValue = '' | ||
function joinRoom(name, roomID) { | ||
socket.emit('join-room', { username: name, room: roomID }, (err) => { | ||
if (err) { | ||
alert("Room doesn't exist") | ||
data = '' | ||
return | ||
} | ||
data = { username: name, room: roomID } | ||
localStorage.setItem(ID_KEY, JSON.stringify(data)) | ||
}) | ||
} | ||
function handleLogin() { | ||
if (!username || !room) return | ||
joinRoom(username, room) | ||
if (!usernameValue || !roomValue) return | ||
joinRoom(usernameValue, roomValue) | ||
} | ||
onMount(() => { | ||
const local = JSON.parse(localStorage.getItem(ID_KEY)) | ||
if (!local) return | ||
joinRoom(local.username, local.room) | ||
}) | ||
</script> | ||
|
||
<Tailwindcss /> | ||
<main class="bg-gray-300 h-screen flex items-center justify-center w-full"> | ||
{#if data} | ||
<ChatRoom {...data} {socket} /> | ||
{#if $loggedUser} | ||
<ChatRoom /> | ||
{:else} | ||
<Login bind:username bind:room {handleLogin} /> | ||
<Login bind:usernameValue bind:roomValue {handleLogin} /> | ||
{/if} | ||
|
||
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import io from 'socket.io-client' | ||
import { writable } from 'svelte/store' | ||
import { ID_KEY } from './const' | ||
|
||
export const users = writable([]) | ||
export const loggedUser = writable() | ||
|
||
export const socket = io('http://localhost:3000', { | ||
transports: ['websocket', 'polling'], | ||
}) | ||
|
||
socket.on('user-disconnect', (leavingUser) => { | ||
users.update((currentUsers) => { | ||
return currentUsers.filter((user) => user.id !== leavingUser.id) | ||
}) | ||
}) | ||
socket.on('user-join', (joinedUser) => { | ||
users.update((currentUsers) => [...currentUsers, joinedUser]) | ||
}) | ||
|
||
export function joinRoom(name, roomID) { | ||
const user = { username: name, room: roomID } | ||
socket.emit('join-room', user, (roomUsers, currentUser) => { | ||
const person = roomUsers.find(({ id }) => id === currentUser.id) | ||
person.isSelf = true | ||
users.set(roomUsers) | ||
loggedUser.set(user) | ||
localStorage.setItem(ID_KEY, JSON.stringify(user)) | ||
}) | ||
} | ||
const saveUser = JSON.parse(localStorage.getItem(ID_KEY)) | ||
if (saveUser) { | ||
joinRoom(saveUser.username, saveUser.room) | ||
} |
Oops, something went wrong.