{"id":2478,"date":"2022-02-14T18:15:48","date_gmt":"2022-02-14T09:15:48","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=2478"},"modified":"2022-10-26T10:10:36","modified_gmt":"2022-10-26T01:10:36","slug":"nuxt-websocket","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2022\/02\/14\/nuxt-websocket\/","title":{"rendered":"Nuxt.js\u3067WebSocket\u3092\u5229\u7528\u3057\u305f\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u901a\u4fe1"},"content":{"rendered":"

\u306f\u3058\u3081\u306b<\/h2>\n

\u4eca\u56de\u3092Nuxt.js\u3092\u5229\u7528\u3057\u305f\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306bWebSocket\u3092\u5c0e\u5165\u3057\u3066\u307f\u305f\u5185\u5bb9\u3092\u5171\u6709\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n

Nuxt.js\u3068\u306f<\/h2>\n

Nuxt.js<\/a>\u306fVue.js<\/a>\u306a\u3069\u3092\u30d9\u30fc\u30b9\u306b\u3057\u305fJavascript\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e00\u7a2e\u3067\u3059\u3002
\nNuxt.js\u306b\u306fVue.js\u306e\u6a5f\u80fd\u306b\u52a0\u3048\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3001Vue Router\u306b\u3088\u308b\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3084Vuex\u306b\u3088\u308b\u72b6\u614b\u7ba1\u7406\u306a\u3069\u3001\u3059\u3050\u306b\u958b\u767a\u7740\u624b\u3067\u304d\u308b\u958b\u767a\u74b0\u5883\u3068\u62e1\u5f35\u6027\u3068\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002
\n(\u203b
Vue.js\u5165\u9580 \u57fa\u790e\u304b\u3089\u5b9f\u8df5\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u307e\u3067<\/a>\u3088\u308a\u4e00\u90e8\u5f15\u7528)<\/p>\n

WebSocket\u3068\u306f<\/h2>\n

WebSocket\u3068\u306f\u30e6\u30fc\u30b6\u30fc\u306e\u30d6\u30e9\u30a6\u30b6\u30fc\u3068\u30b5\u30fc\u30d0\u30fc\u9593\u3067\u5bfe\u8a71\u7684\u306a\u901a\u4fe1\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u958b\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u5148\u9032\u6280\u8853\u3067\u3059\u3002\u3053\u306e API \u306b\u3088\u3063\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3057\u305f\u308a\u3001\u5fdc\u7b54\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u30dd\u30fc\u30ea\u30f3\u30b0\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u30a4\u30d9\u30f3\u30c8\u99c6\u52d5\u578b\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u4fe1\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n(\u203b
MDN Web Docs<\/a>\u3088\u308a\u5f15\u7528)<\/p>\n

\u624b\u9806<\/h2>\n

\u524d\u63d0<\/h3>\n

\u672c\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001Node.js\u30d0\u30fc\u30b8\u30e7\u30f3\u306f16.13.2\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

$ node -v\nv16.13.2<\/code><\/pre>\n

\u307e\u305fJavascript\u30d1\u30c3\u30b1\u30fc\u30b8\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u3068\u3057\u3066yarn<\/code>\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/h3>\n

\u307e\u305a\u306f\u96db\u5f62\u3068\u306a\u308bNuxt.js\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092create-nuxt-app<\/a>\u3092\u4f7f\u7528\u3057\u3066\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u307e\u3059\u3002<\/p>\n

$ yarn create nuxt-app nuxt-websocket-sample<\/code><\/pre>\n

\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u5074<\/h3>\n

\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u5074\u3067WebSocket\u3092\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306b\u3001\u4eca\u56de\u306fws<\/a>\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n

$ yarn add ws<\/code><\/pre>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u5074\u306e\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3059\u3002
\nserver-middleware\/ws.js<\/code><\/p>\n

const WebSocket = require('ws')\n\nconst wss = new WebSocket.Server({ port: 8080 })\n\n\/\/ \u63a5\u7d9a\u51e6\u7406\nwss.on('connection', (ws) => {\n  \/\/ \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u53d6\u3063\u305f\u969b\u306e\u51e6\u7406\n  ws.on('message', (message) => {\n    \/\/ \u9001\u4fe1\u5143\u3092\u542b\u3080\u3059\u3079\u3066\u306eWebSocket\u63a5\u7d9a\u6e08\u307f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u9001\u4fe1\n    wss.clients.forEach((client) => {\n      client.send(message)\n    })\n  })\n})\n\nmodule.exports = { handler: wss }<\/code><\/pre>\n

\u4eca\u56de\u306fnuxt.js\u306e\u6a5f\u69cb\u306b\u5247\u3063\u3066serverMiddleware<\/a>\u3068\u3057\u3066\u30b5\u30fc\u30d0\u30fc\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002
\nnuxt.config.js<\/code>\u306b\u5148\u307b\u3069\u4f5c\u6210\u3057\u305fws.js\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n

export default {\n  \/\/ \u8ffd\u52a0\n  serverMiddleware: [\n    { path: '\/', handler: '~\/server-middleware\/ws.js' }\n  ]\n}<\/code><\/pre>\n

\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074<\/h3>\n

\u9001\u4fe1\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3059\u308b\u3068\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u308b\u7c21\u6613\u306a\u30c1\u30e3\u30c3\u30c8\u6a5f\u80fd\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306fJavascript\u30cd\u30a4\u30c6\u30a3\u30d6\u306a
WebSocket API<\/a>\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002
\n\u306a\u304a\u3001UI\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3057\u3066
Vuetify<\/a>\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

pages\/chat.vue<\/code><\/p>\n

<template>\n  <div>\n    <v-text-field\n      v-model="text"\n      label="text here"\n    \/>\n    <v-btn\n      color="success"\n      @click="onSend"\n    >\n      Send\n    <\/v-btn>\n    <v-list>\n      <v-list-item v-for="(message, i) in messages" :key="i">\n        {{ message }}\n      <\/v-list-item>\n    <\/v-list>\n  <\/div>\n<\/template>\n\n<script>\nexport default {\n  name: 'ChatPage',\n  data () {\n    return {\n      text: '',\n      messages: [],\n      \/\/ WebSocket\u63a5\u7d9a\n      socket: new WebSocket('ws:\/\/localhost:8080')\n    }\n  },\n  mounted () {\n    \/\/ WebSocket\u63a5\u7d9a\u304c\u78ba\u7acb\u3055\u308c\u305f\u6642\u306b\u5b9f\u884c\u3055\u308c\u308b\n    this.socket.onopen = () => console.log('socket connected')\n    \/\/ \u30e1\u30c3\u30bb\u30fc\u30b8\u53d7\u4fe1\u6642\u306e\u51e6\u7406\n    this.socket.onmessage = (event) => {\n      if (!event.data) { return }\n      const message = event.data\n      this.messages.push(message)\n    }\n  },\n  methods: {\n    onSend () {\n      if (!this.text) {\n        return\n      }\n      \/\/ \u30b5\u30fc\u30d0\u30fc\u5074\u306b\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\n      this.socket.send(this.text)\n      this.text = ''\n    }\n  }\n}\n<\/script>\n<\/code><\/pre>\n

\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n

\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u30ed\u30fc\u30ab\u30eb\u7aef\u672b\u4e0a\u3067\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n

$ yarn dev<\/code><\/pre>\n

\u540c\u3058\u753b\u9762\u30922\u3064\u958b\u3044\u305f\u72b6\u614b\u3067\u3001\u7247\u65b9\u306e\u753b\u9762\u304b\u3089\u9001\u4fe1\u3092\u884c\u3046\u3068\u4e21\u65b9\u306e\u753b\u9762\u4e0a\u3067\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u66f4\u65b0\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n

\"\"<\/p>\n

\u958b\u767a\u8005\u30c4\u30fc\u30eb\u4e0a\u3067\u3082WebSocket\u3067\u30c7\u30fc\u30bf\u901a\u4fe1\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002
\n\"\"<\/p>\n

\u53c2\u8003<\/h2>\n

Handling Websockets with Express<\/a>
\n
Svelte + WebSocket\u3067\u30b7\u30f3\u30d7\u30eb\u306a\u30c1\u30e3\u30c3\u30c8\u30a2\u30d7\u30ea\u3092\u4f5c\u308b<\/a>
\n
Nuxt.js\u3067WebSocket\u3092\u4f7f\u3063\u3066\u307f\u308b #nuxtjs #vuejs<\/a>
\n
\u3010Node.js\u3011WebSocket\u3053\u3068\u306f\u3058\u3081\u3010Javascript\u3011<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

\u306f\u3058\u3081\u306b \u4eca\u56de\u3092Nuxt.js\u3092\u5229\u7528\u3057\u305f\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306bWebSocket\u3092\u5c0e\u5165\u3057\u3066\u307f\u305f\u5185\u5bb9\u3092\u5171\u6709\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002 Nuxt.js\u3068\u306f Nuxt.js\u306fVue.js\u306a\u3069\u3092\u30d9\u30fc\u30b9\u306b\u3057\u305fJavascript\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e00\u7a2e\u3067\u3059\u3002 Nuxt.js\u306b\u306fVue.js\u306e\u6a5f\u80fd\u306b\u52a0\u3048\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3001Vue Router\u306b\u3088\u308b\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3084Vuex\u306b\u3088\u308b\u72b6\u614b\u7ba1\u7406\u306a\u3069\u3001\u3059\u3050\u306b\u958b\u767a\u7740\u624b\u3067\u304d\u308b\u958b\u767a […]<\/p>\n","protected":false},"author":10,"featured_media":2482,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[77,138],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/2478"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":30,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/2478\/revisions"}],"predecessor-version":[{"id":2512,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/2478\/revisions\/2512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/2482"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}