<p>Hey all, I'm a new golang user and am interested in writing an example web chat app for gaining experience. I would like to create a lobby where everyone can choose to join a chat room and from there, only be able to communicate with other users in that room.</p>
<p>Getting an example chat web-app running with a single channel / room was fairly easy and straightforward, as there are tons of examples online on Github. However, I'm wondering how to implement the individual rooms or channels part of the server code.</p>
<p><strong>EDIT: I apologize for being unclear in my question. Copy pasta-ing from my comment below:</strong></p>
<blockquote>
<p>I guess more specifically, which way would be more appropriate to organize websocket connections? I know the websocket connection path is defined through the url in javascript, for example something similar to:
new WebSocket('ws://localhost:8080/ws')</p>
<p>So instead of the suffix path /ws in the url, would it be better to generate some unique identifier for creating a new room and pass it in to the websocket url such that it ends up being something along the lines of ws://localhost:8080/ws/UNIQUE_PATH_HERE/ ? So in this sense, the unique path could be considered the "room or channel id" (akin to ws://localhost:8080/ws/ROOM_ID/). My question for this approach would be how to have the server recognize the appended ROOM_ID, with maybe some kind of wildcard approach (/ws/*)...</p>
<p>Or would it be better to have every client connect to the same websocket url (like simply just ws://localhost:8080/ws for all connections) and have the server abstractly define rooms and what connections are in the same room for communication?</p>
</blockquote>
<hr/>**评论:**<br/><br/>Decateron: <pre><p>I don't really think this is a go-specific problem, I imagine you'd approach this in the same way in any language. The server just needs to keep track of what room each user is in and only broadcast messages which are sent by users in that room to other users within the room. </p></pre>xlzqwerty1: <pre><p>I guess more specifically, which way would be more appropriate to organize websocket connections? I know the websocket connection path is defined through the url in javascript, for example something similar to:</p>
<p>new WebSocket('ws://localhost:8080/ws')</p>
<p>So instead of the suffix path /ws in the url, would it be better to generate some unique identifier for creating a new room and pass it in to the websocket url such that it ends up being something along the lines of ws://localhost:8080/ws/UNIQUE_PATH_HERE/ ? So in this sense, the unique path could be considered the "room or channel id" (akin to ws://localhost:8080/ws/ROOM_ID/). My question for this approach would be how to have the server recognize the appended ROOM_ID, with maybe some kind of wildcard approach (/ws/*)...</p>
<p>Or would it be better to have every client connect to the same websocket url (like simply just ws://localhost:8080/ws for all connections) and have the server abstractly define rooms and what connections are in the same room for communication? </p></pre>wheey: <pre><p>By your explanation, and from my point of view, it does not matter, if user is already connected to server you can just add him to requested group without adding additional ROOM_ID in url as room will be another channel.</p>
<p>One fast google touch:
<a href="https://github.com/TapirLiu/golang_chat" rel="nofollow">https://github.com/TapirLiu/golang_chat</a></p></pre>PacNinja: <pre><p>Made a similar project while back <a href="https://github.com/godwhoa/wsrooms" rel="nofollow">https://github.com/godwhoa/wsrooms</a> it's fairly simple and commented so hope you find it helpful.</p></pre>xlzqwerty1: <pre><p>Thank you very much, this will be quite helpful.</p></pre>jns111: <pre><p>If I get you right this is what you are looking for:
<a href="https://github.com/kataras/go-websocket" rel="nofollow">https://github.com/kataras/go-websocket</a></p></pre>danredux: <pre><p>You're thinking of it like a http request, but it's not.</p>
<p>Have one websocket url that everyone connects to, but when they connect they are in a "lobby" on the server. Then the server sends them a list of rooms, and the client says "connect me to room X". On the server you just say something like <code>rooms[X].Add(client)</code>, then the room will start handling the websocket messages instead of the lobby. The room should first send a message to say "I've accepted you", then start sending chats from that room. If someone says something in that room, loop through the other clients in the room and send the message to them.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传