강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

GroundP님의 프로필 이미지
GroundP

작성한 질문수

탄탄한 백엔드 NestJS, 기초부터 심화까지

HTTP vs Socket

io is not defined

해결된 질문

작성

·

1.3K

2

 

const socket = io('/');
const getElementById = (id=> document.getElementById(id|| null;

// get DOM element
const helloStrangerElement = getElementById('hello_stranger');
const chattingBoxElement = getElementById('chatting_box');
const formElement = getElementById('chat_form');

function helloUser() {
  const userName = prompt('What is your name?');
  socket.emit('new_user'userName);
  console.log(userName);
}

function init() {
  helloUser();
}

init();

 

io is not defined라고 뜨면서 prompt가 나오지 않습니다. scripts.js의 1라인을 주석철히하면 prompt는 뜨구요. 

 

어떻게 하면 해결할 수 있을까요?

(https://github.com/GroundP/random-chatting)

 

 

답변 1

1

윤상석님의 프로필 이미지
윤상석
지식공유자

안녕하세요!

https://github.com/GroundP/random-chatting/blob/master/views/index.hbs

이 부분이 문제인 것 같습니다!

scripts.js은 socket script 아래에 있어야 합니다! (scripts.js에서 socket 라이브러리를 사용하기 때문에) 지금 위 아래 둘 다 scripts.js가 삽입되어 있는데 위에 부분 지워주시면 됩니다!

GroundP님의 프로필 이미지
GroundP
질문자

감사합니다!

GroundP님의 프로필 이미지
GroundP

작성한 질문수

질문하기